Skip to content

Package: DetailBar

DetailBar

nameinstructionbranchcomplexitylinemethod
DetailBar(ConfigurationGraph)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getNameText()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
reassignAttributes()
M: 0 C: 26
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
refreshDetails(Item)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: package networkconfigurator;
2:
3: import java.awt.Component;
4: import java.awt.Dimension;
5: import java.awt.event.FocusEvent;
6: import java.awt.event.FocusListener;
7: import java.util.Iterator;
8:
9: import javax.swing.JLabel;
10: import javax.swing.JTextField;
11: import javax.swing.JToolBar;
12: import javax.swing.event.DocumentEvent;
13: import javax.swing.event.DocumentListener;
14:
15: import model.assignment.VariableAssignment;
16: import model.type.ReferenceIsUnresolvedException;
17: import model.type.TypeDoesNotExistException;
18: import model.type.TypeExceptions;
19: import model.type.ValueNotInRangeOfTypeException;
20: import networkconfigurator.item.ConfigRouter;
21: import networkconfigurator.item.HardwareEdge;
22: import networkconfigurator.item.Host;
23: import networkconfigurator.item.Internet;
24: import networkconfigurator.item.Item;
25: import networkconfigurator.item.ItemVisitor;
26: import networkconfigurator.item.NetworkAdapter;
27: import networkconfigurator.item.NetworkCable;
28: import networkconfigurator.item.Router;
29: import networkconfigurator.item.Slot;
30: import networkconfigurator.item.Switch;
31:
32: /**
33: * Detail bar of device nodes.
34: *
35: * @author Erik Arand
36: *
37: */
38: public class DetailBar extends JToolBar implements IDetailBar {
39:         /**
40:          * the graph.
41:          */
42:         private final ConfigurationGraph graph;
43:         /**
44:          * Label for the name.
45:          */
46:         private final JLabel nameLabel = new JLabel("Name");
47:         /**
48:          * Textfield for the name.
49:          */
50:         private final JTextField nameText = new JTextField("name");
51:
52:         /**
53:          * The constructor.
54:          *
55:          * @param graph
56:          * the graph.
57:          */
58:         DetailBar(final ConfigurationGraph graph) {
59:                 this.setOrientation(VERTICAL);
60:                 this.add(nameLabel);
61:                 this.add(nameText);
62:                 this.setVisible(false);
63:                 this.graph = graph;
64:         }
65:
66:         /**
67:          * fills the DetailBar with attributes of the selected item.
68:          *
69:          * @param item
70:          * the item.
71:          */
72:         public void refreshDetails(final Item item) {
73:
74:                 this.removeAll();
75:                 this.revalidate();
76:                 this.repaint();
77:
78:                 final Iterator<DetailBarPropertyEntry> iterator = item.getPropertyBar().iterator();
79:•                while (iterator.hasNext()) {
80:                         this.add(iterator.next().getPropertyBarEntry().accept(new CreateDetailBar()));
81:                 }
82:         }
83:
84:         /**
85:          * Methode for refreshing the textfields.
86:          */
87:         @Override
88:         public void reassignAttributes() {
89:                 Item.getSelectedNodes(graph.getItems(), graph.getSelected());
90:•                if (graph.getSelected().size() == 1) {
91:                         graph.getSelected().get(0).accept(new SetNameVisitor());
92:                 }
93:         }
94:
95:         /**
96:          * Üblicher Getter für das Attribut nameText.
97:          *
98:          * @return liefert nameText.
99:          */
100:         public JTextField getNameText() {
101:                 return nameText;
102:         }
103:
104:         /**
105:          * create the detail bar menu.
106:          *
107:          * @author Jannik
108:          *
109:          */
110:         private final class CreateDetailBar implements DetailBarVisitor {
111:                 @Override
112:                 public Component visit(final DetailBarLabel value) {
113:                         return new JLabel(value.getLabelVaule());
114:                 }
115:
116:                 @Override
117:                 public Component visit(final VariableAssignment value) {
118:                         final JTextField jTextField = new JTextField(value.getValue());
119:                         prepareTestField(jTextField);
120:
121:                         jTextField.getDocument()
122:                                         .addDocumentListener(new FillJTextFieldWithVa(value, jTextField));
123:
124:                         jTextField.addFocusListener(new FocusListener() {
125:                                 @Override
126:                                 public void focusLost(final FocusEvent e) {
127:                                         try {
128:                                                 value.setValue(jTextField.getText());
129:                                         } catch (IllegalArgumentException | ValueNotInRangeOfTypeException
130:                                                         | TypeDoesNotExistException | TypeExceptions e1) {
131:                                                 // TODO maybe a window could show all error messages separately. So
132:                                                 // the user
133:                                                 // could also see, which errors at the VariableAssignment still have
134:                                                 // to be
135:                                                 // processed.
136:                                         }
137:                                 }
138:
139:                                 @Override
140:                                 public void focusGained(final FocusEvent e) {
141:                                 }
142:                         });
143:
144:                         jTextField.getDocument()
145:                                         .addDocumentListener(new FillJTextFieldWithVa(value, jTextField));
146:
147:                         jTextField.addFocusListener(new FocusListener() {
148:                                 @Override
149:                                 public void focusLost(final FocusEvent e) {
150:                                         try {
151:                                                 value.setValue(jTextField.getText());
152:                                         } catch (IllegalArgumentException | ValueNotInRangeOfTypeException
153:                                                         | TypeDoesNotExistException | TypeExceptions e1) {
154:                                                 // TODO maybe a window could show all error messages separately. So
155:                                                 // the user
156:                                                 // could also see, which errors at the VariableAssignment still have
157:                                                 // to be
158:                                                 // processed.
159:                                         }
160:                                 }
161:
162:                                 @Override
163:                                 public void focusGained(final FocusEvent e) {
164:                                 }
165:                         });
166:
167:                         return jTextField;
168:                 }
169:
170:                 @Override
171:                 public Component visit(final DetailInputBox detailBarInputBox) {
172:                         final JTextField jTextField = new JTextField(detailBarInputBox.getContentValue());
173:                         prepareTestField(jTextField);
174:
175:                         jTextField.addFocusListener(new FocusListener() {
176:                                 @Override
177:                                 public void focusLost(final FocusEvent e) {
178:                                         detailBarInputBox.setContentValue(jTextField.getText());
179:                                 }
180:
181:                                 @Override
182:                                 public void focusGained(final FocusEvent e) {
183:                                 }
184:                         });
185:
186:                         return jTextField;
187:                 }
188:
189:                 /**
190:                  * gives the text field a fixed width and center the text.
191:                  *
192:                  * @param jTextField
193:                  * the text field which should be modified.
194:                  */
195:                 private void prepareTestField(final JTextField jTextField) {
196:                         final Dimension dNumber = new Dimension(150, 30);
197:                         jTextField.setPreferredSize(dNumber);
198:                         jTextField.setHorizontalAlignment(JTextField.CENTER);
199:                 }
200:         }
201:
202:         /**
203:          * fill a jtextfield with the variable assignment reference.
204:          *
205:          * @author Jannik
206:          *
207:          */
208:         private final class FillJTextFieldWithVa implements DocumentListener {
209:                 /**
210:                  * the reference to the variable assignment.
211:                  */
212:                 private final VariableAssignment va;
213:                 /**
214:                  * the jTextField.
215:                  */
216:                 private final JTextField jTextField;
217:
218:                 /**
219:                  *
220:                  * @param va
221:                  * the variable assignment.
222:                  * @param jTextField
223:                  * the jTextField which should be filled.
224:                  */
225:                 private FillJTextFieldWithVa(final VariableAssignment va, final JTextField jTextField) {
226:                         this.va = va;
227:                         this.jTextField = jTextField;
228:                 }
229:
230:                 @Override
231:                 public void insertUpdate(final DocumentEvent e) {
232:                         validation();
233:
234:                 }
235:
236:                 @Override
237:                 public void removeUpdate(final DocumentEvent e) {
238:                         validation();
239:
240:                 }
241:
242:                 @Override
243:                 public void changedUpdate(final DocumentEvent e) {
244:                 }
245:
246:                 /**
247:                  * validate the input.
248:                  *
249:                  */
250:                 private void validation() {
251:                         try {
252:                                 if (va.checkValue(jTextField.getText())) {
253:                                         jTextField.setBackground(NetworkconfiguratorConstants.LIGHTGREEN);
254:                                 } else {
255:                                         jTextField.setBackground(NetworkconfiguratorConstants.LIGHTRED);
256:                                 }
257:                         } catch (IllegalArgumentException | ReferenceIsUnresolvedException e1) {
258:                                 // TODO maybe a window could show all error messages separately. So
259:                                 // the user
260:                                 // could also see, which errors at the VariableAssignment still have
261:                                 // to be
262:                                 // processed.
263:                         }
264:                 }
265:         }
266:
267:         /**
268:          * Adapter for setting the textfields.
269:          *
270:          * @author Erik Arand
271:          *
272:          */
273:         private class SetNameVisitor implements ItemVisitor {
274:
275:                 @Override
276:                 public void internet(final Internet internet) {
277:                 }
278:
279:                 @Override
280:                 public void adapter(final NetworkAdapter adapter) {
281:                 }
282:
283:                 @Override
284:                 public void configRouter(final ConfigRouter router) {
285:                         router.setName(nameText.getText());
286:                 }
287:
288:                 @Override
289:                 public void host(final Host host) {
290:                         host.setName(nameText.getText());
291:                 }
292:
293:                 @Override
294:                 public void router(final Router router) {
295:                         router.setName(nameText.getText());
296:                 }
297:
298:                 @Override
299:                 public void sWitch(final Switch sWitch) {
300:                         sWitch.setName(nameText.getText());
301:                 }
302:
303:                 @Override
304:                 public void hardwareEdge(final HardwareEdge edge) {
305:                 }
306:
307:                 @Override
308:                 public void networkCable(final NetworkCable edge) {
309:                 }
310:
311:                 @Override
312:                 public void slot(final Slot slot) {
313:                 }
314:         };
315:
316: }