1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /*
19 * Project: KernelConfigurator
20 * Created on 03-mar-2004
21 *
22 * Copyright (c)2003 Grid Systems
23 */
24 package com.gridsystems.config.tools.console;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ResourceBundle;
30
31 /**
32 * A Field that acts as a container for other fields.
33 *
34 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
35 * @version 1.0
36 */
37 public class GroupField extends Field {
38 /**
39 * Group title.
40 */
41 protected String title;
42
43 /**
44 * Collection of fields.
45 */
46 protected List<Field> fields = new ArrayList<Field>();
47
48 /**
49 * Creates a new instance with an empty field list.
50 *
51 * @param bundle The resource bundle to use
52 * @param title The bundle key for the title
53 */
54 public GroupField(ResourceBundle bundle, String title) {
55 this(bundle, title, null);
56 }
57
58 /**
59 * Creates a new instance with the specified list of fields.
60 *
61 * @param bundle The resource bundle to use
62 * @param title The bundle key for the title
63 * @param fields The initial field list
64 */
65 public GroupField(ResourceBundle bundle, String title, List<Field> fields) {
66 super(bundle);
67 if (fields == null) {
68 fields = new ArrayList<Field>();
69 }
70 this.title = title;
71 this.fields = new ArrayList<Field>(fields);
72 }
73
74 /**
75 * Adds another field to the list.
76 *
77 * @param field The field to add
78 */
79 public void addField(Field field) {
80 this.fields.add(field);
81 }
82
83 /**
84 * Removes the field at position <code>pos</code> from the list.
85 *
86 * @param pos the position of the field to remove
87 * @return the removed field
88 */
89 public Field removeField(int pos) {
90 return (Field)this.fields.remove(pos);
91 }
92
93 /**
94 * Clears all fields in the list.
95 */
96 public void clearFields() {
97 this.fields.clear();
98 }
99
100 /**
101 * Gets the list of fields in this group.
102 *
103 * @return the list of fields
104 */
105 public List getFields() {
106 return this.fields;
107 }
108
109 /**
110 * Gets the list of actions in this group.
111 *
112 * @return The list of actions in this group.
113 */
114 public ConsoleViewAction[] getActions() {
115 List<ConsoleViewAction> list = getActionList();
116
117 ConsoleViewAction[] actions = new ConsoleViewAction[list.size()];
118 list.toArray(actions);
119 return actions;
120 }
121
122 /**
123 * Gets the list of actions in this group.
124 *
125 * @return the list of actions in this group
126 */
127 public List<ConsoleViewAction> getActionList() {
128 ArrayList<ConsoleViewAction> list = new ArrayList<ConsoleViewAction>();
129 for (Iterator it = fields.iterator(); it.hasNext();) {
130 Field field = (Field)it.next();
131 if (field instanceof ConsoleViewAction) {
132 list.add((ConsoleViewAction)field);
133 }
134 }
135 return list;
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 public String[] getContents() {
142 ArrayList<String> list = new ArrayList<String>();
143
144 list.add(getString(title));
145
146 for (Iterator it = fields.iterator(); it.hasNext();) {
147 Field f = (Field)it.next();
148 String[] lines = f.getContents();
149 for (int i = 0; i < lines.length; i++) {
150 list.add(lines[i]);
151 }
152 }
153 String[] contents = new String[list.size()];
154 list.toArray(contents);
155 return contents;
156 }
157 }