1 /*--------------------------------------------------------------------------
2 * Copyright 2004 Taro L. Saito
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *--------------------------------------------------------------------------*/
16 //--------------------------------------
17 // Phenome Commons Project
19 // OptionComposite.java
24 //--------------------------------------
25 package org.xerial.util.cui;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31 import java.util.List;
36 * A base class for {@link OptionWithNoArgument} and {@link OptionGroup}
41 abstract class OptionBase<OptionID extends Comparable>
43 abstract protected OptionWithNoArgument<OptionID> findByLongOptionName(String longOption);
44 abstract protected OptionWithNoArgument<OptionID> findByShortOptionName(String shortOption);
45 abstract protected OptionWithNoArgument<OptionID> findOption(OptionID optionID);
46 abstract protected void collectOptionID(List<OptionID> optionIDList);
47 abstract protected void collectOptionDescriptions(OptionDescriptionContainer container);
49 protected OptionBase(OptionGroup<OptionID> parentGroup)
51 setParent(parentGroup);
54 protected void setParent(OptionGroup<OptionID> parentGroup)
56 _parentGroup = parentGroup;
58 protected OptionGroup<OptionID> getParent()
63 protected void activateParentGroup()
65 OptionGroup<OptionID> parent = getParent();
72 private OptionGroup<OptionID> _parentGroup = null;
78 * A container class that holds help messages
82 class OptionDescriptionContainer
85 public void addDescription(String shortOptionColumn, String longOptionColumn, String descriptionColumn)
87 String column[] = new String[3];
88 column[0] = shortOptionColumn;
89 column[1] = longOptionColumn;
90 column[2] = descriptionColumn;
91 _columnList.add(column);
93 public void addDescription(String groupName)
95 String singleColumn[] = new String[1];
96 singleColumn[0] = groupName;
97 _columnList.add(singleColumn);
100 public String toString()
102 // calculate necessary width for each column
103 int width[] = { 0, 0, 0 };
104 for (Iterator ci = _columnList.iterator(); ci.hasNext();)
106 String[] line = (String[]) ci.next();
107 if (line.length != 3)
108 continue; // single line
109 for (int i = 0; i < line.length; i++)
110 width[i] = width[i] < line[i].length() ? line[i].length() : width[i];
112 for(int i=0; i<width.length; i++)
114 // print each options
115 StringWriter strWriter = new StringWriter();
116 PrintWriter out = new PrintWriter(strWriter);
117 for (Iterator ci = _columnList.iterator(); ci.hasNext();)
119 String[] line = (String[]) ci.next();
121 out.print(line[0]); // group name
124 out.print(" "); /// left margin
125 for (int i = 0; i < line.length; i++)
128 int numSpace = width[i] - line[i].length();
129 for (int j = 0; j < numSpace; j++)
135 return strWriter.toString();
138 LinkedList<String[]> _columnList = new LinkedList<String[]>();