1 19 package org.openharmonise.him.editors.preview; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 24 import javax.swing.*; 25 26 import org.openharmonise.commons.xml.*; 27 import org.openharmonise.vfs.context.*; 28 import org.openharmonise.vfs.gui.*; 29 import org.w3c.dom.*; 30 31 32 38 public class StateDialog 39 extends JDialog 40 implements LayoutManager, ActionListener, ContextListener, MouseListener { 41 42 private JButton m_okButton = null; 43 private JButton m_cancelButton = null; 44 private JTextArea m_stateLabel = null; 45 private JList m_stateList = null; 46 private XMLPrettyPrint m_printer = null; 47 48 private StateContainer m_stateContainer = null; 49 50 public StateDialog() { 51 m_stateContainer = new StateContainer(); 52 } 53 56 public StateDialog(Frame frame, String title, String sVFilePath) { 57 super(frame, title, true); 58 ContextHandler.getInstance().addListener( 59 ContextType.CONTEXT_SHUTDOWN, 60 this); 61 m_stateContainer = new StateContainer(sVFilePath); 62 Document doc = m_stateContainer.getMemberState(sVFilePath); 64 if (doc != null) { 65 m_stateContainer.setState(doc); 66 } else { 67 setup(); 68 } 69 } 70 private void setup() { 71 m_printer = new XMLPrettyPrint(); 72 73 this.getContentPane().setLayout(this); 74 this.setSize(200, 300); 75 76 m_stateList = new JList(); 77 refreshJList(); 78 getContentPane().add(m_stateList); 79 m_stateList.addMouseListener(this); 80 81 this.m_okButton = new JButton("OK"); 82 this.m_okButton.setActionCommand("OK"); 83 this.m_okButton.addActionListener(this); 84 m_okButton.setEnabled(false); 85 this.getContentPane().add(m_okButton); 86 87 this.m_cancelButton = new JButton("Cancel"); 88 this.m_cancelButton.setActionCommand("CANCEL"); 89 this.m_cancelButton.addActionListener(this); 90 this.getContentPane().add(m_cancelButton); 91 92 this.m_stateLabel = new JTextArea("Select a state"); 93 m_stateLabel.setOpaque(false); 94 this.getContentPane().add(this.m_stateLabel); 95 96 int x = 97 this.getGraphicsConfiguration().getBounds().width / 2 98 - this.getSize().width / 2; 99 int y = 100 this.getGraphicsConfiguration().getBounds().height / 2 101 - this.getSize().height / 2; 102 103 this.setLocation(x, y); 104 105 } 106 109 public void removeLayoutComponent(Component arg0) { 110 } 111 114 public void layoutContainer(Container arg0) { 115 m_stateList.setSize(170, 200); 116 m_stateList.setLocation(10, 30); 117 118 m_stateLabel.setSize(170,20); 119 m_stateLabel.setLocation(10,10); 120 121 this.m_okButton.setSize(70, 20); 122 this.m_okButton.setLocation(10, 240); 123 124 this.m_cancelButton.setSize(90, 20); 125 this.m_cancelButton.setLocation(90, 240); 126 } 127 130 public void addLayoutComponent(String arg0, Component arg1) { 131 } 132 135 public Dimension minimumLayoutSize(Container arg0) { 136 return null; 137 } 138 141 public Dimension preferredLayoutSize(Container arg0) { 142 return null; 143 } 144 147 public void actionPerformed(ActionEvent ae) { 148 if (ae.getActionCommand().equals("OK")) { 149 doOK(); 150 } else if (ae.getActionCommand().equals("CANCEL")) { 151 doCancel(); 152 } 153 } 154 private void doOK() { 155 String scenario = (String ) m_stateList.getSelectedValue(); 156 if (scenario != null) { 157 m_stateContainer.addMemberToScenario(scenario); 158 m_stateContainer.save(); 159 this.setVisible(false); 160 } 161 } 162 private void doCancel() { 163 m_stateContainer.setState(null); 164 this.hide(); 165 } 166 public void refreshJList() { 167 this.m_stateList.setListData(m_stateContainer.getStates().keySet().toArray()); 168 } 169 172 public Document getState() { 173 return m_stateContainer.getState(); 174 } 175 public void contextMessage(ContextEvent ce) { 176 if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SHUTDOWN) { 177 m_stateContainer.save(); 178 } 179 } 180 183 public void mouseClicked(MouseEvent me) { 184 if (me.getClickCount() > 0 && me.getComponent() == m_stateList) { 185 m_okButton.setEnabled(true); 186 int index = this.m_stateList.locationToIndex(me.getPoint()); 187 String name = (String ) m_stateList.getSelectedValue(); 188 Document stateDoc = m_stateContainer.getState(name); 189 if (me.getClickCount() == 2) { 190 doOK(); 191 } 192 } 193 194 } 195 198 public void mouseEntered(MouseEvent arg0) { 199 } 200 203 public void mouseExited(MouseEvent arg0) { 204 } 205 208 public void mousePressed(MouseEvent arg0) { 209 } 210 213 public void mouseReleased(MouseEvent arg0) { 214 } 215 public static void main(String [] args) { 216 Frame frame = new Frame(); 217 frame.setIconImage( 218 ((ImageIcon) IconManager 219 .getInstance() 220 .getIcon("16-page-definition.gif")) 221 .getImage()); 222 StateDialog sd = new StateDialog(frame, "state selector", "/webdav/Content/bob"); 223 Document doc = sd.getState(); 225 if(doc == null){ 226 sd.show(); 227 doc = sd.getState(); 228 } 229 if (doc != null) { 230 System.out.println(doc.getDocumentElement().toString()); 231 } 232 233 System.exit(0); 234 } 235 } 236 | Popular Tags |