001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mlm.debug.ui;
028:
029: import java.awt.Color;
030: import java.awt.Component;
031: import java.awt.Container;
032: import java.awt.Dimension;
033: import java.awt.GridBagConstraints;
034: import java.awt.GridBagLayout;
035: import java.awt.Insets;
036: import java.awt.LayoutManager;
037: import java.awt.Toolkit;
038: import java.awt.event.ActionEvent;
039: import java.awt.event.ActionListener;
040: import java.awt.event.WindowAdapter;
041: import java.awt.event.WindowEvent;
042: import java.awt.event.WindowListener;
043: import java.util.Vector;
044:
045: import javax.swing.JButton;
046: import javax.swing.JComboBox;
047: import javax.swing.JFrame;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import javax.swing.JScrollPane;
051: import javax.swing.JTextField;
052:
053: import org.cougaar.glm.ldm.Constants;
054: import org.cougaar.glm.ldm.plan.Capability;
055: import org.cougaar.planning.ldm.plan.ClusterObjectFactory;
056: import org.cougaar.planning.ldm.plan.Plan;
057: import org.cougaar.planning.plugin.legacy.PluginDelegate;
058:
059: /** Supports task creation by users.
060: */
061:
062: public class UserInput implements ActionListener, Runnable {
063: static String CREATE_TASK = "Create Task";
064: static String SET_DESTINATION_CLUSTER = "Set Destination Cluster: ";
065: static String SET_VERB = "Verb: ";
066: static String SET_DIRECT_OBJECT_CLUSTER_ASSET = "Direct Object Cluster Asset: ";
067: static String SET_DIRECT_OBJECT_PHYSICAL_ASSET = "Direct Object Physical Asset: ";
068: static String SET_DIRECT_OBJECT_ASSET_QUANTITY = "Direct Object Asset Quantity: ";
069: static String SET_DIRECT_OBJECT_CAPABILITY = "Direct Object Capabilities: ";
070: static String SET_PHRASE_PREPOSITION = "Phrase Preposition: ";
071: static String SET_PHRASE_CLUSTER_ASSET = "Phrase Cluster Asset: ";
072: static String SET_PHRASE_PHYSICAL_ASSET = "Phrase Physical Asset: ";
073: static String SET_PHRASE_ASSET_QUANTITY = "Phrase Asset Quantity: ";
074: static String SET_PHRASE_CAPABILITY = "Phrase Capabilities: ";
075: static String SET_START_DATE = "Set Start Date: ";
076: static String SET_END_DATE = "Set End Date: ";
077: static String SET_BEST_DATE = "Set Best Date: ";
078: String sourceCluster = "Commander"; // temporary, should be this cluster
079: // set from user input
080: String destinationCluster = "";
081: String verb = "";
082: String objectClusterAsset = "";
083: String objectPhysicalAsset = "";
084: String objectCapability = "";
085: Vector objectCapabilities = new Vector(4);
086: String phrasePreposition = "";
087: String phraseClusterAsset = "";
088: String phrasePhysicalAsset = "";
089: String phraseCapability = "";
090: Vector phraseCapabilities = new Vector(4);
091: String defaultStartDate = "September 01, 1998 8:00 AM";
092: String defaultEndDate = "December 31, 1998 8:00 AM";
093: String defaultBestDate = "September 01, 1998 8:00 AM";
094: String startDate = defaultStartDate;
095: String endDate = defaultEndDate;
096: String bestDate = defaultBestDate;
097: int objectAssetQuantity = 0;
098: int phraseAssetQuantity = 0;
099: int WIDTH = 600; // width and height of frame
100: int HEIGHT = 500;
101: JComboBox box;
102: JTextField startDateField;
103: JTextField endDateField;
104: JTextField bestDateField;
105: ScrollingTextLine scrollingTextLine;
106: int gridx = 0; // for grid bag layout
107: int gridy = 0;
108: Insets noInternalPadding;
109: Insets internalPadding;
110: Insets labelInternalPadding;
111: JPanel panel;
112: UIPlugin uiPlugin;
113: UIDisplay uiDisplay;
114: ClusterObjectFactory cof;
115: Plan realityPlan;
116: String planName;
117: PluginDelegate delegate;
118:
119: /** Initialize values used by user input thread.
120: @param uiPlugin this plugin
121: @param uiDisplay the main display object
122: */
123: public UserInput(UIPlugin uiPlugin, UIDisplay uiDisplay,
124: PluginDelegate delegate) {
125: this .uiPlugin = uiPlugin;
126: this .uiDisplay = uiDisplay;
127: this .delegate = delegate;
128: cof = delegate.getFactory();
129: realityPlan = cof.getRealityPlan();
130: planName = realityPlan.getPlanName();
131: }
132:
133: /** Create the user input frame which allows the user to input
134: tasks, specifying each field in the task.
135: */
136:
137: public void run() {
138: Vector choices;
139:
140: // create window
141: WindowListener windowListener = new WindowAdapter() {
142: public void windowClosing(WindowEvent e) {
143: e.getWindow().dispose();
144: }
145: };
146: JFrame frame = new JFrame("COUGAAR User Input");
147: frame.setForeground(Color.black);
148: frame.setBackground(Color.lightGray);
149: frame.addWindowListener(windowListener);
150: frame.setSize(WIDTH, HEIGHT);
151: Dimension screenSize = Toolkit.getDefaultToolkit()
152: .getScreenSize();
153: frame.setLocation(screenSize.width / 2 - WIDTH / 2,
154: screenSize.height / 2 - HEIGHT / 2);
155:
156: // physical assets
157: Vector physicalAssets = new Vector(3);
158: physicalAssets.addElement("Truck");
159: physicalAssets.addElement("Solenoid");
160:
161: // capabilities
162: Vector capabilities = new Vector(6);
163: capabilities.addElement(Capability.SPAREPARTSPROJECTOR);
164: capabilities.addElement(Capability.SPAREPARTSPROVIDER);
165: capabilities.addElement(Capability.SUPERIOR);
166: capabilities.addElement(Capability.SPAREPART);
167: capabilities.addElement(Capability.MAJORENDITEM);
168: capabilities.addElement(Capability.SUBORDINATE);
169:
170: // verb
171: Vector verbs = new Vector(11);
172: verbs.addElement(Constants.Verb.TRANSPORT);
173: verbs.addElement(Constants.Verb.MAINTAIN);
174: verbs.addElement(Constants.Verb.SUPPLY);
175: verbs.addElement(Constants.Verb.ARM);
176: verbs.addElement(Constants.Verb.FUEL);
177: verbs.addElement(Constants.Verb.GETLOGSUPPORT);
178: verbs.addElement(Constants.Verb.MANAGE);
179: verbs.addElement(Constants.Verb.DETERMINEREQUIREMENTS);
180: verbs.addElement(Constants.Verb.USERINPUT);
181: verbs.addElement(Constants.Verb.SUPPORTREQUEST);
182: verbs.addElement(Constants.Verb.REPORTFORDUTY);
183: verbs.addElement(Constants.Verb.REPORTFORSERVICE);
184: verbs.addElement(Constants.Verb.TRANSPORTATIONMISSION);
185:
186: // prepositional phrases: enumeration of phrase
187: Vector prepositions = new Vector(5);
188: prepositions.addElement(Constants.Preposition.WITH);
189: prepositions.addElement(Constants.Preposition.TO);
190: prepositions.addElement(Constants.Preposition.FROM);
191: prepositions.addElement(Constants.Preposition.FOR);
192:
193: // do layout
194: panel = new JPanel();
195: GridBagLayout gbl = new GridBagLayout();
196: panel.setLayout(gbl);
197: internalPadding = new Insets(2, 10, 2, 10);
198: noInternalPadding = new Insets(0, 0, 0, 0);
199: labelInternalPadding = new Insets(0, 100, 0, 0);
200:
201: addClusterComboBox(SET_DESTINATION_CLUSTER);
202: addComboBox(SET_VERB, verbs);
203: addClusterComboBox(SET_DIRECT_OBJECT_CLUSTER_ASSET);
204: addComboBox(SET_DIRECT_OBJECT_PHYSICAL_ASSET, physicalAssets);
205: addComboBox(SET_DIRECT_OBJECT_CAPABILITY, capabilities);
206: addTextField(SET_DIRECT_OBJECT_ASSET_QUANTITY, 6, "");
207: addComboBox(SET_PHRASE_PREPOSITION, prepositions);
208: addClusterComboBox(SET_PHRASE_CLUSTER_ASSET);
209: addComboBox(SET_PHRASE_PHYSICAL_ASSET, physicalAssets);
210: addComboBox(SET_PHRASE_CAPABILITY, capabilities);
211: addTextField(SET_PHRASE_ASSET_QUANTITY, 6, "");
212: startDateField = addTextField(SET_START_DATE, 20,
213: defaultStartDate);
214: endDateField = addTextField(SET_END_DATE, 20, defaultEndDate);
215: bestDateField = addTextField(SET_BEST_DATE, 20, defaultBestDate);
216:
217: // display the task that the user is composing
218: gridx = 0;
219: scrollingTextLine = new ScrollingTextLine(50);
220: addComponent(panel, scrollingTextLine, gridx, gridy++,
221: GridBagConstraints.REMAINDER, 1,
222: GridBagConstraints.CENTER, GridBagConstraints.NONE, 0,
223: 0, internalPadding, 0, 0);
224:
225: // button to create new task
226: gridx = 0;
227: JButton button = new JButton(CREATE_TASK);
228: button.setActionCommand(CREATE_TASK);
229: button.addActionListener(this );
230: addComponent(panel, button, gridx, gridy++,
231: GridBagConstraints.REMAINDER, 1,
232: GridBagConstraints.CENTER, GridBagConstraints.NONE, 0,
233: 0, internalPadding, 0, 0);
234:
235: JScrollPane scroller = new JScrollPane();
236: scroller.getViewport().add(panel);
237: frame.getContentPane().add("Center", scroller);
238: frame.setVisible(true);
239: }
240:
241: private void addComponent(Container container, Component component,
242: int gridx, int gridy, int gridwidth, int gridheight,
243: int anchor, int fill, int ipadx, int ipady, Insets insets,
244: double weightx, double weighty) {
245: LayoutManager gbl = container.getLayout();
246: GridBagConstraints gbc = new GridBagConstraints();
247: gbc.gridx = gridx;
248: gbc.gridy = gridy;
249: gbc.gridwidth = gridwidth;
250: gbc.gridheight = gridheight;
251: gbc.fill = fill;
252: gbc.anchor = anchor;
253: gbc.ipadx = ipadx;
254: gbc.ipady = ipady;
255: gbc.insets = insets;
256: gbc.weightx = weightx;
257: gbc.weighty = weighty;
258: ((GridBagLayout) gbl).setConstraints(component, gbc);
259: container.add(component);
260: }
261:
262: private void addComboBox(String actionCommand, Vector choices) {
263: gridx = 0;
264: JLabel label = new JLabel(actionCommand);
265: addComponent(panel, label, gridx++, gridy, 1, 1,
266: GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
267: 0, 0, labelInternalPadding, 1, 0);
268: JComboBox box = new JComboBox();
269: for (int i = 0; i < choices.size(); i++)
270: box.addItem(choices.elementAt(i));
271: box.addItem("");
272: box.setSelectedIndex(choices.size()); // unspecified to start
273: box.setEditable(true); // allow user to add their own choices
274: box.setActionCommand(actionCommand);
275: box.addActionListener(this );
276: addComponent(panel, box, gridx++, gridy++, 1, 1,
277: GridBagConstraints.WEST, GridBagConstraints.NONE, 0, 0,
278: internalPadding, 1, 0);
279: }
280:
281: private void addClusterComboBox(String actionCommand) {
282: gridx = 0;
283: JLabel label = new JLabel(actionCommand);
284: addComponent(panel, label, gridx++, gridy, 1, 1,
285: GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
286: 0, 0, labelInternalPadding, 1, 0);
287: JComboBox box = new JComboBox();
288: Vector choices = uiPlugin.getClusterNames();
289: for (int i = 0; i < choices.size(); i++)
290: box.addItem(choices.elementAt(i));
291: box.addItem("");
292: box.setSelectedIndex(box.getItemCount() - 1); // unspecified to start
293: box.setActionCommand(actionCommand);
294: box.addActionListener(this );
295: addComponent(panel, box, gridx++, gridy++, 1, 1,
296: GridBagConstraints.WEST, GridBagConstraints.NONE, 0, 0,
297: internalPadding, 1, 0);
298: }
299:
300: private JTextField addTextField(String title, int nColumns,
301: String init) {
302: gridx = 0;
303: JLabel label = new JLabel(title);
304: addComponent(panel, label, gridx++, gridy, 1, 1,
305: GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL,
306: 0, 0, labelInternalPadding, 1, 0);
307: JTextField textField = new JTextField(nColumns);
308: textField.setActionCommand(title); // title and action command are the same
309: textField.addActionListener(this );
310: textField.setText(init);
311: addComponent(panel, textField, gridx++, gridy++, 1, 1,
312: GridBagConstraints.WEST, GridBagConstraints.NONE, 0, 0,
313: internalPadding, 1, 0);
314: return textField;
315: }
316:
317: private void displayTask() {
318: String objectQuantity = "";
319: String phraseQuantity = "";
320: if (objectAssetQuantity != 0)
321: objectQuantity = String.valueOf(objectAssetQuantity);
322: if (phraseAssetQuantity != 0)
323: phraseQuantity = String.valueOf(phraseAssetQuantity);
324: String s1 = "";
325: for (int i = 0; i < objectCapabilities.size(); i++)
326: s1 = s1 + objectCapabilities.elementAt(i);
327: String s2 = "";
328: for (int i = 0; i < phraseCapabilities.size(); i++)
329: s2 = s2 + phraseCapabilities.elementAt(i);
330: String s = planName + " " + sourceCluster + "->"
331: + destinationCluster + " " + verb + " "
332: + objectClusterAsset + " " + objectPhysicalAsset + " "
333: + s1 + " " + objectQuantity + " " + phrasePreposition
334: + " " + phraseClusterAsset + " " + phrasePhysicalAsset
335: + " " + s2 + " " + phraseQuantity + " " + startDate
336: + " " + endDate + " " + bestDate;
337: s.trim();
338: scrollingTextLine.setText(s);
339: }
340:
341: private void resetForm(JPanel panel) {
342: int count = panel.getComponentCount();
343: for (int i = 0; i < count; i++) {
344: Component component = panel.getComponent(i);
345: if (component instanceof JPanel)
346: resetForm((JPanel) component);
347: if (component instanceof JComboBox)
348: ((JComboBox) component).setSelectedItem("");
349: else if (component instanceof JTextField) {
350: JTextField textField = (JTextField) component;
351: if (textField.isEditable()) {
352: if (textField.equals(startDateField))
353: textField.setText(defaultStartDate);
354: else if (textField.equals(endDateField))
355: textField.setText(defaultEndDate);
356: else if (textField.equals(bestDateField))
357: textField.setText(defaultBestDate);
358: else
359: textField.setText("");
360: }
361: scrollingTextLine.setText("");
362: }
363: }
364: }
365:
366: private void initDefaults() {
367: destinationCluster = "";
368: verb = "";
369: objectClusterAsset = "";
370: objectPhysicalAsset = "";
371: objectCapability = "";
372: objectCapabilities = new Vector(4);
373: phrasePreposition = "";
374: phraseClusterAsset = "";
375: phrasePhysicalAsset = "";
376: phraseCapability = "";
377: phraseCapabilities = new Vector(4);
378: objectAssetQuantity = 0;
379: phraseAssetQuantity = 0;
380: startDate = defaultStartDate;
381: endDate = defaultEndDate;
382: bestDate = defaultBestDate;
383: }
384:
385: /** User made a selection. We need to set the value of the appropriate
386: task object and add the user's input to the task displayed.
387: */
388:
389: public void actionPerformed(ActionEvent e) {
390: String command = e.getActionCommand();
391: Object source = e.getSource();
392:
393: if (source instanceof JButton) {
394: if (command.equals(CREATE_TASK)) {
395: UserInputTask newTask = new UserInputTask(uiPlugin,
396: delegate, destinationCluster,
397: objectClusterAsset, objectPhysicalAsset,
398: objectCapabilities, objectAssetQuantity,
399: phrasePreposition, phraseClusterAsset,
400: phrasePhysicalAsset, phraseCapabilities,
401: phraseAssetQuantity, verb, startDate, endDate,
402: bestDate);
403: newTask.addToLogPlan();
404: initDefaults();
405: resetForm(panel);
406: }
407: } else if (source instanceof JComboBox) {
408: JComboBox box = (JComboBox) source;
409: Object o = box.getSelectedItem();
410: String s = "";
411: if (o instanceof String)
412: s = (String) o;
413: // if user added new value, then add it to list of choices
414: if (box.getSelectedIndex() == -1)
415: box.addItem(s);
416: if (command.equals(SET_DESTINATION_CLUSTER))
417: destinationCluster = s;
418: else if (command.equals(SET_DIRECT_OBJECT_CLUSTER_ASSET))
419: objectClusterAsset = s;
420: else if (command.equals(SET_PHRASE_CLUSTER_ASSET))
421: phraseClusterAsset = s;
422: else if (command.equals(SET_VERB))
423: verb = s;
424: else if (command.equals(SET_DIRECT_OBJECT_PHYSICAL_ASSET))
425: objectPhysicalAsset = s;
426: else if (command.equals(SET_DIRECT_OBJECT_CAPABILITY)) {
427: if (!s.equals(""))
428: objectCapabilities.addElement(s);
429: } else if (command.equals(SET_PHRASE_PREPOSITION))
430: phrasePreposition = s;
431: else if (command.equals(SET_PHRASE_PHYSICAL_ASSET))
432: phrasePhysicalAsset = s;
433: else if (command.equals(SET_PHRASE_CAPABILITY)) {
434: if (!s.equals(""))
435: phraseCapabilities.addElement(s);
436: }
437: } else if (source instanceof JTextField) {
438: JTextField textField = (JTextField) source;
439: String s = textField.getText();
440: if (command.equals(SET_DIRECT_OBJECT_ASSET_QUANTITY))
441: objectAssetQuantity = Integer.parseInt(s);
442: else if (command.equals(SET_PHRASE_ASSET_QUANTITY))
443: phraseAssetQuantity = Integer.parseInt(s);
444: else if (command.equals(SET_START_DATE))
445: startDate = s;
446: else if (command.equals(SET_END_DATE))
447: endDate = s;
448: else if (command.equals(SET_BEST_DATE))
449: bestDate = s;
450: }
451: displayTask();
452: }
453:
454: }
|