001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.ui.experiment;
028:
029: import org.cougaar.tools.csmart.core.db.CMT;
030: import org.cougaar.tools.csmart.core.db.DBUtils;
031: import org.cougaar.tools.csmart.core.db.ExperimentDB;
032: import org.cougaar.tools.csmart.experiment.DBExperiment;
033: import org.cougaar.tools.csmart.experiment.Experiment;
034:
035: import javax.swing.*;
036: import java.awt.*;
037: import java.util.ArrayList;
038: import java.util.Map;
039: import java.util.Set;
040:
041: public class ThreadBuilder extends JPanel {
042: // thread names displayed for user
043: private static final String[] ULThreads = {
044: "Subsistence (Class 1)", "Fuel (Class 3)",
045: "Construction Material (Class 4)", "Ammunition (Class 5)",
046: "Spare Parts (Class 9)" };
047: // thread names passed to database will be CMT.ULDBThreads
048: private String[] groupNames;
049: private ArrayList groupCheckBoxes = new ArrayList();
050: private ArrayList ULThreadCheckBoxes = new ArrayList();
051: private ArrayList multiplierFields = new ArrayList();
052: private ArrayList originalThreadSelected = new ArrayList();
053: private ArrayList originalGroupSelected = new ArrayList();
054: private ArrayList originalGroupMultiplier = new ArrayList();
055: private Experiment experiment;
056: private boolean needUpdate = true; // need to update display
057:
058: public ThreadBuilder(Experiment experiment) {
059: this .experiment = experiment;
060: updateDisplay();
061: needUpdate = false;
062: }
063:
064: /**
065: * Called when this panel is first displayed after having been
066: * created or re-initialized to display a new experiment.
067: * Initially, this panel is used only for viewing, so all the controls
068: * are disabled; eventually this can be used for configuring the experiment.
069: */
070:
071: private void updateDisplay() {
072: removeAll(); // remove previous components
073: if (experiment == null
074: || !DBUtils.dbMode
075: || !ExperimentDB.isExperimentNameInDatabase(experiment
076: .getExperimentName())) {
077: setLayout(new BorderLayout());
078: add(
079: new JLabel(
080: "Experiment is not in database or otherwise has no thread information available.",
081: SwingConstants.CENTER), BorderLayout.CENTER);
082: return;
083: }
084: String experimentId = experiment.getExperimentID();
085: String trialId = experiment.getTrialID();
086: setLayout(new GridBagLayout());
087: int x = 0;
088: int y = 0;
089: int leftIndent = 0;
090: add(new JLabel("Threads:"), new GridBagConstraints(x, y++, 1,
091: 1, 0.0, 0.0, GridBagConstraints.WEST,
092: GridBagConstraints.NONE,
093: new Insets(0, leftIndent, 5, 0), 0, 0));
094: leftIndent = leftIndent + 5;
095: for (int i = 0; i < ULThreads.length; i++) {
096: JCheckBox cb = new JCheckBox(ULThreads[i]);
097: boolean sel = ExperimentDB.isULThreadSelected(trialId,
098: CMT.ULDBThreads[i]);
099: ULThreadCheckBoxes.add(cb);
100: cb.setSelected(sel);
101: originalThreadSelected.add(new Boolean(sel));
102: cb.setEnabled(false);
103: add(cb, new GridBagConstraints(x, y++, 1, 1, 0.0, 0.0,
104: GridBagConstraints.WEST, GridBagConstraints.NONE,
105: new Insets(0, leftIndent, 5, 0), 0, 0));
106: }
107:
108: ////////////////////////////
109: // Now the OrgGroups
110:
111: Map groupNameToId = ExperimentDB
112: .getOrganizationGroups(experimentId);
113: Set groups = groupNameToId.keySet();
114: groupNames = (String[]) groups
115: .toArray(new String[groups.size()]);
116: int nGroupNames = groupNames.length;
117:
118: // Show the groups stuff only if we have some
119: if (nGroupNames > 0) {
120: leftIndent = leftIndent - 5;
121: add(new JLabel("Organization Groups:"),
122: new GridBagConstraints(x, y++, 1, 1, 0.0, 0.0,
123: GridBagConstraints.WEST,
124: GridBagConstraints.NONE, new Insets(0,
125: leftIndent, 0, 0), 0, 0));
126: leftIndent = leftIndent + 5;
127: for (int i = 0; i < nGroupNames; i++) {
128: String groupName = (String) groupNames[i];
129: JCheckBox groupCB = new JCheckBox(groupName);
130: boolean sel = ExperimentDB.isGroupSelected(trialId,
131: groupName);
132: groupCheckBoxes.add(groupCB);
133: originalGroupSelected.add(new Boolean(sel));
134: groupCB.setSelected(sel);
135: groupCB.setEnabled(false);
136: add(groupCB, new GridBagConstraints(x++, y, 1, 1, 0.0,
137: 0.0, GridBagConstraints.WEST,
138: GridBagConstraints.NONE, new Insets(0,
139: leftIndent, 5, 0), 0, 0));
140: add(new JLabel("Number of Copies:"),
141: new GridBagConstraints(x++, y, 1, 1, 0.0, 0.0,
142: GridBagConstraints.WEST,
143: GridBagConstraints.NONE, new Insets(0,
144: 0, 5, 5), 0, 0));
145: JTextField multiplierField = new JTextField(4);
146: multiplierFields.add(multiplierField);
147: int multiplier = ExperimentDB.getMultiplier(trialId,
148: groupName);
149: multiplierField.setText(String.valueOf(multiplier));
150: multiplierField.setEnabled(false);
151: originalGroupMultiplier.add(new Integer(multiplier));
152: add(multiplierField, new GridBagConstraints(x++, y, 1,
153: 1, 0.0, 0.0, GridBagConstraints.WEST,
154: GridBagConstraints.NONE,
155: new Insets(0, 0, 5, 5), 0, 0));
156: add(new JLabel("Members:"), new GridBagConstraints(x++,
157: y, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
158: GridBagConstraints.NONE,
159: new Insets(0, 0, 5, 5), 0, 0));
160: Set members = ExperimentDB.getOrganizationsInGroup(
161: experimentId, (String) groupNameToId
162: .get(groupName));
163: JList membersList = new JList(members.toArray());
164: membersList.setVisibleRowCount(4);
165: membersList.setEnabled(false);
166: add(new JScrollPane(membersList),
167: new GridBagConstraints(x++, y++, 1, 1, 0.0,
168: 0.0, GridBagConstraints.WEST,
169: GridBagConstraints.NONE, new Insets(0,
170: 0, 5, 5), 0, 0));
171: } // loop over group names
172: } // end of block to show groups if have some
173: }
174:
175: /**
176: * Set experiment to display. Display is actually updated
177: * when this panel is made visible.
178: * TODO: why does this have to reset the editable and runnable
179: * flags -- this is done by the caller (ExperimentBuilder)
180: */
181:
182: public void reinit(Experiment newExperiment) {
183: if (this .experiment != null
184: && this .experiment.equals(newExperiment))
185: return; // no change
186: experiment = newExperiment;
187: if (isShowing())
188: updateDisplay();
189: else
190: needUpdate = true;
191: }
192:
193: /**
194: * Ensure that display is up-to-date before showing it.
195: */
196:
197: public void setVisible(boolean visible) {
198: if (visible && needUpdate)
199: updateDisplay();
200: super.setVisible(visible);
201: }
202:
203: }
|