001: /*
002: * CLIF is a Load Injection Framework
003: * Copyright (C) 2007 France Telecom
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * CLIF Jordan BRUNIER Grégory CALONNIER
020: *
021: * Contact: clif@objectweb.org
022: */
023: package salomeTMF_plug.clif.ui;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Container;
027: import java.awt.Dimension;
028: import java.awt.FlowLayout;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.KeyEvent;
032: import java.awt.event.WindowAdapter;
033: import java.awt.event.WindowEvent;
034: import java.io.File;
035: import java.io.FileInputStream;
036: import java.io.FileOutputStream;
037: import java.util.ArrayList;
038: import java.util.Iterator;
039: import java.util.Map;
040:
041: import javax.swing.BorderFactory;
042: import javax.swing.ImageIcon;
043: import javax.swing.JButton;
044: import javax.swing.JDesktopPane;
045: import javax.swing.JDialog;
046: import javax.swing.JFrame;
047: import javax.swing.JMenu;
048: import javax.swing.JMenuBar;
049: import javax.swing.JMenuItem;
050: import javax.swing.JOptionPane;
051: import javax.swing.JPanel;
052: import javax.swing.JTextField;
053: import javax.swing.KeyStroke;
054: import javax.swing.Timer;
055: import javax.swing.border.TitledBorder;
056:
057: import org.objectweb.clif.console.lib.ClifDeployDefinition;
058: import org.objectweb.clif.console.lib.TestPlanReader;
059: import org.objectweb.clif.console.lib.TestPlanWriter;
060: import org.objectweb.clif.console.lib.gui.GuiAboutDialog;
061: import org.objectweb.clif.console.lib.gui.GuiConsoleImpl;
062: import org.objectweb.clif.console.lib.gui.TestPlanWindow;
063: import org.objectweb.clif.supervisor.api.BladeState;
064: import org.objectweb.clif.util.ExecutionContext;
065: import org.objectweb.clif.util.gui.GuiAlert;
066: import org.objectweb.util.monolog.Monolog;
067: import org.objectweb.util.monolog.api.BasicLevel;
068: import org.objectweb.util.monolog.api.Logger;
069:
070: public class ClifTestPlanEditor implements ActionListener {
071:
072: static final String CLIF_APPLICATION = "org.objectweb.clif.console.lib.gui.ClifApp";
073:
074: static public int clifWidth = 900;
075: static public int clifHeight = 700;
076:
077: // GUI commands
078: static private final String ABOUT_CMD = "about";
079: static private final String EXIT_CMD = "exit";
080: static private final String SAVE_CMD = "save";
081: static private final String ADD_CMD = "add";
082: //static private CtpFilter ctpFilter;
083:
084: // logger
085: static private Logger log;
086:
087: private BladeState globalState = BladeState.UNDEPLOYED;
088: //private ClifRegistry clifReg;
089: private String test;
090: private JFrame frame;
091: private JMenuBar menuBar;
092: private JMenuItem itemAbout;
093: private TestPlanWindow testPlanFrame;
094: private JDesktopPane desktop;
095: private long ellapsedTime;
096: private long lastStartTime;
097: private Timer ellapsedTimeTimer;
098:
099: protected ArrayList<String> servers = new ArrayList<String>();
100:
101: public ClifTestPlanEditor(String test) {
102: ExecutionContext.init("./");
103: log = Monolog.getMonologFactory().getLogger(
104: ClifTestPlanEditor.class.getName());
105:
106: globalState = BladeState.UNDEPLOYED;
107: ImageIcon clifIcon = new ImageIcon(GuiConsoleImpl.class
108: .getClassLoader().getResource("icons/clificon.png"));
109:
110: frame = new JFrame("CLIF is a Load Injection Framework");
111: frame.setIconImage(clifIcon.getImage());
112: frame.setSize(clifWidth, clifHeight);
113: frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
114: frame.addWindowListener(new WindowCloser());
115:
116: menuBar = new JMenuBar();
117: JMenu menu;
118: JMenuItem item;
119:
120: // File menu
121: menu = new JMenu("File");
122:
123: item = new JMenuItem("Add a server");
124: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,
125: ActionEvent.CTRL_MASK));
126: item.setActionCommand(ADD_CMD);
127: menu.add(item).addActionListener(this );
128:
129: item = new JMenuItem("Save this test plan");
130: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
131: ActionEvent.CTRL_MASK));
132: item.setActionCommand(SAVE_CMD);
133: menu.add(item).addActionListener(this );
134:
135: menu.addSeparator();
136: item = new JMenuItem("Quit");
137: item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
138: ActionEvent.CTRL_MASK));
139: item.setActionCommand(EXIT_CMD);
140: menu.add(item).addActionListener(this );
141: menuBar.add(menu);
142:
143: frame.setJMenuBar(menuBar);
144:
145: // About menu
146: menu = new JMenu("?");
147: itemAbout = new JMenuItem("About...");
148: itemAbout.setActionCommand(ABOUT_CMD);
149: itemAbout.addActionListener(this );
150: menu.add(itemAbout);
151: menuBar.add(menu);
152:
153: desktop = new JDesktopPane();
154: testPlanFrame = new TestPlanWindow(frame);
155: testPlanFrame.setFrameIcon(clifIcon);
156: testPlanFrame.setVisible(true);
157: testPlanFrame.setLocation(0, 0);
158: //testPlanFrame.setAvailableServers(clifReg.getServers());
159:
160: try {
161: Map tp = TestPlanReader.readFromProp(new FileInputStream(
162: new File(test)));
163: Iterator iter = tp.entrySet().iterator();
164: servers.add(ExecutionContext.DEFAULT_SERVER);
165: while (iter.hasNext()) {
166: Map.Entry entry = (Map.Entry) iter.next();
167: ClifDeployDefinition def = (ClifDeployDefinition) entry
168: .getValue();
169: if (!servers.contains(def.getServerName())) {
170: servers.add(def.getServerName());
171: }
172: }
173: } catch (Exception e) {
174:
175: }
176: testPlanFrame.setAvailableServers((String[]) servers
177: .toArray(new String[0]));
178:
179: //testPlanFrame.setAvailableServers(new String[]{"Local Host"});
180: desktop.add(testPlanFrame);
181:
182: try {
183: testPlanFrame.setTestPlan(TestPlanReader
184: .readFromProp(new FileInputStream(new File(test))));
185: this .test = test;
186: } catch (Exception ex) {
187: String message = "Can't open test plan" + test;
188: System.out.println(message);
189: log.log(BasicLevel.ERROR, message, ex);
190: new GuiAlert(desktop, "Can't open test plan", ex.toString())
191: .alert();
192: }
193:
194: frame.getContentPane().add(desktop);
195: frame.show();
196: testPlanFrame.setSize(desktop.getWidth(), desktop.getHeight());
197:
198: //ellapsedTimeTimer = new Timer(1000, this);
199: }
200:
201: class WindowCloser extends WindowAdapter {
202: public void windowClosing(WindowEvent e) {
203: if (testPlanFrame.isEmpty()
204: || JOptionPane
205: .showConfirmDialog(
206: null,
207: "Did you think about saving your test plan !\nContinue to leave ?",
208: "Warning",
209: JOptionPane.OK_CANCEL_OPTION,
210: JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
211: frame.dispose();
212: }
213: }
214: }
215:
216: //////////////////////////////
217: // interface ActionListener //
218: //////////////////////////////
219:
220: public void actionPerformed(ActionEvent e) {
221: String command = e.getActionCommand();
222:
223: if (e.getSource() == ellapsedTimeTimer) {
224: testPlanFrame.setStatusLine(globalState, globalState
225: .equals(BladeState.RUNNING) ? ellapsedTime
226: + System.currentTimeMillis() - lastStartTime
227: : ellapsedTime);
228: } else if (command.equals(ADD_CMD)) {
229: new GuiAddServer(frame).ask();
230: } else if (command.equals(SAVE_CMD)) {
231: try {
232: //currentDirectory = filechooser.getSelectedFile().getParent();
233: Map testPlan = testPlanFrame.getTestPlan();
234: if (testPlan != null) {
235: TestPlanWriter.write2prop(
236: new FileOutputStream(test), testPlan);
237:
238: JOptionPane.showMessageDialog(null,
239: "Current test plan saved.", "Information",
240: JOptionPane.INFORMATION_MESSAGE);
241: }
242: } catch (Exception ex) {
243: System.out.println("Can't save test plan : " + test);
244: ex.printStackTrace();
245: log.log(BasicLevel.INFO, "Can't save test plan", ex);
246: }
247: } else if (command.equals(EXIT_CMD)
248: && (testPlanFrame.isEmpty() || JOptionPane
249: .showConfirmDialog(
250: null,
251: "Did you think about saving your test plan !\nContinue to leave ?",
252: "Warning",
253: JOptionPane.OK_CANCEL_OPTION,
254: JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)) {
255: //clifApp.stop(null);
256: //clifApp.deploy(null, clifReg);
257: this .frame.dispose();
258: } else if (command.equals(ABOUT_CMD)) {
259: new GuiAboutDialog(this .frame).setVisible(true);
260: }
261: }
262:
263: ///////////
264: // class //
265: ///////////
266:
267: class GuiAddServer extends JDialog implements ActionListener {
268: protected JButton addBtn, cancelBtn;
269: protected JTextField serverName;
270: protected boolean value = false;
271:
272: GuiAddServer(JFrame frame) {
273: super (frame, "Add a server", true);
274:
275: Container pane = getContentPane();
276: pane.setLayout(new BorderLayout());
277: serverName = new JTextField();
278: serverName.setPreferredSize(new Dimension(150, 19));
279: JPanel jpTop = new JPanel();
280: FlowLayout fl = new FlowLayout();
281: fl.setAlignment(FlowLayout.LEFT);
282: jpTop.setLayout(fl);
283:
284: jpTop.setBorder(BorderFactory.createTitledBorder(null,
285: "New server's name :",
286: TitledBorder.DEFAULT_JUSTIFICATION,
287: TitledBorder.DEFAULT_POSITION, null, null));
288: jpTop.add(serverName);
289: pane.add(BorderLayout.CENTER, jpTop);
290:
291: JPanel buttonPnl = new JPanel();
292: buttonPnl.add(addBtn = new JButton("Add"));
293: addBtn.addActionListener(this );
294: buttonPnl.add(cancelBtn = new JButton("Cancel"));
295: cancelBtn.addActionListener(this );
296: pane.add(BorderLayout.SOUTH, buttonPnl);
297:
298: addWindowListener(new WindowCloser());
299: this .setResizable(false);
300:
301: // window center
302: this .setLocationRelativeTo(this .getParent());
303: }
304:
305: public boolean ask() {
306: pack();
307: this .setVisible(true);
308: return value;
309: }
310:
311: public void actionPerformed(ActionEvent e) {
312: if (e.getSource() == addBtn) {
313: if (!serverName.getText().trim().equals("")) {
314: servers.add(serverName.getText());
315: testPlanFrame
316: .setAvailableServers((String[]) servers
317: .toArray(new String[0]));
318: value = true;
319: this .dispose();
320: } else {
321: JOptionPane.showMessageDialog(this ,
322: "Server's name is empty.");
323: }
324: } else if (e.getSource() == cancelBtn) {
325: this .dispose();
326: }
327: }
328:
329: class WindowCloser extends WindowAdapter {
330: public void windowClosing(WindowEvent e) {
331: GuiAddServer.this.dispose();
332: }
333: }
334: }
335: }
|