OSDN Git Service

moved the implementation-specific codes to impl package
[xerial/xerial-core.git] / src / main / java / org / xerial / util / cui / OptionBase.java
1 /*--------------------------------------------------------------------------
2  *  Copyright 2004 Taro L. Saito
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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
18 //
19 // OptionComposite.java
20 // Since: 2005/01/03
21 //
22 // $URL$ 
23 // $Author$
24 //--------------------------------------
25 package org.xerial.util.cui;
26
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;
32
33
34 /**
35  * 
36  * A base class for {@link OptionWithNoArgument} and {@link OptionGroup}
37  * 
38  * @author leo
39  *
40  */
41 abstract class OptionBase<OptionID extends Comparable> 
42 {
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);
48     
49     protected OptionBase(OptionGroup<OptionID> parentGroup)
50     {
51         setParent(parentGroup);
52     }
53     
54     protected void setParent(OptionGroup<OptionID> parentGroup)
55     {
56         _parentGroup = parentGroup;
57     }
58     protected OptionGroup<OptionID> getParent()
59     {
60         return _parentGroup;
61     }
62     
63     protected void activateParentGroup()
64     {
65         OptionGroup<OptionID> parent = getParent();
66         if(parent != null)
67             parent.activate();
68     }
69     
70
71     
72     private OptionGroup<OptionID> _parentGroup = null;
73 }
74
75
76
77 /**
78  * A container class that holds help messages
79  * @author leo
80  *
81  */
82 class OptionDescriptionContainer
83 {
84
85     public void addDescription(String shortOptionColumn, String longOptionColumn, String descriptionColumn)
86     {
87         String column[] = new String[3];
88         column[0] = shortOptionColumn;
89         column[1] = longOptionColumn;
90         column[2] = descriptionColumn;
91         _columnList.add(column);
92     }
93     public void addDescription(String groupName)
94     {
95         String singleColumn[] = new String[1];
96         singleColumn[0] = groupName;
97         _columnList.add(singleColumn);
98     }
99
100     public String toString()
101     {
102         // calculate necessary width for each column
103         int width[] = { 0, 0, 0 };
104         for (Iterator ci = _columnList.iterator(); ci.hasNext();)
105         {
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];
111         }
112         for(int i=0; i<width.length; i++)
113             width[i]++;
114         // print each options
115         StringWriter strWriter = new StringWriter();
116         PrintWriter out = new PrintWriter(strWriter);
117         for (Iterator ci = _columnList.iterator(); ci.hasNext();)
118         {
119             String[] line = (String[]) ci.next();
120             if(line.length == 1)
121                 out.print(line[0]);  // group name
122             else
123             {
124                 out.print(" "); /// left margin
125                 for (int i = 0; i < line.length; i++)
126                 {
127                     out.print(line[i]);
128                     int numSpace = width[i] - line[i].length();
129                     for (int j = 0; j < numSpace; j++)
130                         out.print(" ");
131                 }   
132             }
133             out.println();
134         }
135         return strWriter.toString();
136     }
137
138     LinkedList<String[]> _columnList = new LinkedList<String[]>();
139 }
140
141
142