1 19 package org.openharmonise.him.editors.pagegui; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.awt.Dialog ; 24 import java.awt.Dimension ; 25 import java.awt.Frame ; 26 import java.awt.GraphicsConfiguration ; 27 import java.awt.HeadlessException ; 28 import java.awt.LayoutManager ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.io.StringReader ; 32 33 import javax.swing.BorderFactory ; 34 import javax.swing.JButton ; 35 import javax.swing.JDialog ; 36 import javax.swing.JLabel ; 37 import javax.swing.JSpinner ; 38 import javax.swing.SpinnerModel ; 39 import javax.swing.SpinnerNumberModel ; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 42 import org.openharmonise.commons.xml.*; 43 import org.openharmonise.him.*; 44 import org.openharmonise.him.harmonise.*; 45 import org.openharmonise.him.swing.resourcetree.*; 46 import org.openharmonise.vfs.*; 47 import org.openharmonise.vfs.context.*; 48 import org.openharmonise.vfs.gui.*; 49 import org.openharmonise.vfs.servers.ServerList; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 import org.w3c.dom.Node ; 53 import org.w3c.dom.NodeList ; 54 import org.w3c.dom.Text ; 55 56 57 64 public class PageDefinitionDialog extends JDialog implements LayoutManager , ActionListener , ContextListener { 65 66 69 private JButton m_okButton = null; 70 71 74 private JButton m_cancelButton = null; 75 76 79 private JButton m_addButton = null; 80 81 84 private JButton m_removeButton = null; 85 86 89 private ResourceTree m_tree = null; 90 91 94 private ResourceDisplayPanel m_xmlResourceDisplay = null; 95 96 99 private ResourceDisplayPanel m_xslResourceDisplay = null; 100 101 104 private JSpinner m_spinner = null; 105 106 109 private JLabel m_dialogLabel = null; 110 111 114 private JLabel m_xmlLabel = null; 115 116 119 private JLabel m_xslLabel = null; 120 121 124 private JLabel m_timeoutLabel = null; 125 126 129 private boolean m_bCancelled = false; 130 131 132 139 public PageDefinitionDialog(Frame arg0, String arg1) throws HeadlessException { 140 super(arg0, arg1, true); 141 this.setup(); 142 } 143 144 148 private void setup() { 149 ContextHandler.getInstance().addListener(ContextType.CONTEXT_APP_FOCUS, this); 150 this.getContentPane().setLayout(this); 151 152 this.m_okButton = new JButton ("OK"); 153 this.m_okButton.setActionCommand("OK"); 154 this.m_okButton.addActionListener(this); 155 this.getContentPane().add(m_okButton); 156 157 this.m_cancelButton = new JButton ("Cancel"); 158 this.m_cancelButton.setActionCommand("CANCEL"); 159 this.m_cancelButton.addActionListener(this); 160 this.getContentPane().add(m_cancelButton); 161 162 this.m_addButton = new JButton (); 163 this.m_addButton.setActionCommand("ADD"); 164 this.m_addButton.setIcon( IconManager.getInstance().getIcon("16-command-right.png") ); 165 this.m_addButton.addActionListener(this); 166 this.getContentPane().add(this.m_addButton); 167 168 this.m_removeButton = new JButton (); 169 this.m_removeButton.setActionCommand("REMOVE"); 170 this.m_removeButton.setIcon( IconManager.getInstance().getIcon("16-command-left.png") ); 171 this.m_removeButton.addActionListener(this); 172 this.getContentPane().add(this.m_removeButton); 173 174 this.m_tree = new ResourceTree(); 175 this.m_tree.setBorder(BorderFactory.createEtchedBorder()); 176 this.m_tree.setShowLeafNodes(true); 177 this.m_tree.setShowApprovedOnly(true); 178 AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS(); 179 this.m_tree.addCollection(vfs.getVirtualFile( HarmonisePaths.PATH_PAGE_TEMPLATES ).getResource()); 180 this.m_tree.addCollection(vfs.getVirtualFile( HarmonisePaths.PATH_XSLT).getResource()); 181 this.getContentPane().add(this.m_tree); 182 183 this.m_xmlResourceDisplay = new ResourceDisplayPanel(); 184 this.getContentPane().add(this.m_xmlResourceDisplay); 185 186 this.m_xslResourceDisplay = new ResourceDisplayPanel(); 187 this.getContentPane().add(this.m_xslResourceDisplay); 188 189 SpinnerModel model = new SpinnerNumberModel (-1, -1, 100000, 1); 190 this.m_spinner = new JSpinner (model); 191 this.m_spinner.setEditor(new JSpinner.NumberEditor (this.m_spinner)); 192 this.getContentPane().add(this.m_spinner); 193 194 this.m_dialogLabel = new JLabel ("Select the Page Template, XSLT and a timeout value for this Page Definition."); 195 this.getContentPane().add(this.m_dialogLabel); 196 197 this.m_xmlLabel = new JLabel ("Page Template"); 198 this.getContentPane().add(this.m_xmlLabel); 199 200 this.m_xslLabel = new JLabel ("XSLT"); 201 this.getContentPane().add(this.m_xslLabel); 202 203 this.m_timeoutLabel = new JLabel ("Timeout (seconds)"); 204 this.getContentPane().add(this.m_timeoutLabel); 205 206 this.setSize(450,300); 207 208 int x = this.getGraphicsConfiguration().getBounds().width/2-this.getSize().width/2; 209 int y = this.getGraphicsConfiguration().getBounds().height/2-this.getSize().height/2; 210 211 this.setLocation(x, y); 212 213 214 } 215 216 219 public void actionPerformed(ActionEvent ae) { 220 if(ae.getActionCommand().equals("OK")) { 221 this.hide(); 222 } else if(ae.getActionCommand().equals("CANCEL")) { 223 this.m_bCancelled = true; 224 this.hide(); 225 } else if(ae.getActionCommand().equals("ADD")) { 226 VirtualFile vfFile = this.m_tree.getSelectedResource(); 227 if(vfFile.getFullPath().startsWith(HarmonisePaths.PATH_PAGE_TEMPLATES) && !vfFile.isDirectory()) { 228 this.m_xmlResourceDisplay.setVirtualFile(vfFile); 229 } else if(vfFile.getFullPath().startsWith(HarmonisePaths.PATH_XSLT) && !vfFile.isDirectory()) { 230 this.m_xslResourceDisplay.setVirtualFile(vfFile); 231 } 232 } else if(ae.getActionCommand().equals("REMOVE")) { 233 if(this.m_xmlResourceDisplay.isSelected()) { 234 this.m_xmlResourceDisplay.setVirtualFile(null); 235 } 236 if(this.m_xslResourceDisplay.isSelected()) { 237 this.m_xslResourceDisplay.setVirtualFile(null); 238 } 239 } 240 } 241 242 245 public void layoutContainer(Container arg0) { 246 this.m_dialogLabel.setSize(420,20); 247 this.m_dialogLabel.setLocation(10, 10); 248 249 this.m_tree.setSize(200,230); 250 this.m_tree.setLocation(10, 30); 251 252 this.m_addButton.setSize(20, 20); 253 this.m_addButton.setLocation(220, 90); 254 255 this.m_removeButton.setSize(20, 20); 256 this.m_removeButton.setLocation(220, 130); 257 258 this.m_okButton.setSize(70, 20); 259 this.m_okButton.setLocation(270, 240); 260 261 this.m_cancelButton.setSize(70, 20); 262 this.m_cancelButton.setLocation(350, 240); 263 264 this.m_xmlLabel.setSize(100,20); 265 this.m_xmlLabel.setLocation(270, 50); 266 267 this.m_xmlResourceDisplay.setSize(150, 20); 268 this.m_xmlResourceDisplay.setLocation(270, 70); 269 270 this.m_xslLabel.setSize(100,20); 271 this.m_xslLabel.setLocation(270, 130); 272 273 this.m_xslResourceDisplay.setSize(150, 20); 274 this.m_xslResourceDisplay.setLocation(270, 150); 275 276 this.m_timeoutLabel.setSize(100,20); 277 this.m_timeoutLabel.setLocation(270, 180); 278 279 this.m_spinner.setSize(80, 20); 280 this.m_spinner.setLocation(270, 200); 281 } 282 283 286 public void contextMessage(ContextEvent ce) { 287 if(ce.CONTEXT_TYPE==ContextType.CONTEXT_APP_FOCUS) { 288 this.toFront(); 289 } 290 } 291 292 297 public void setData(byte[] data) { 298 AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS(); 299 300 try { 301 Document xml = 302 DocumentBuilderFactory 303 .newInstance() 304 .newDocumentBuilder() 305 .parse( 306 new org.xml.sax.InputSource (new StringReader ( new String (data) ))); 307 308 Element rootEl = xml.getDocumentElement(); 309 310 int nTimeout = -1; 311 if(rootEl.hasAttribute("timeout")) { 312 nTimeout = Integer.parseInt( rootEl.getAttribute("timeout") ); 313 this.m_spinner.getModel().setValue(new Integer (nTimeout)); 314 } 315 316 NodeList nl = rootEl.getChildNodes(); 317 for(int i=0; i<nl.getLength(); i++) { 318 Node node = nl.item(i); 319 if(node.getNodeType()==Node.ELEMENT_NODE) { 320 Element element = (Element )node; 321 if(element.getTagName().equals("XML")) { 322 NodeList nl2 = element.getChildNodes(); 323 for(int j=0; j<nl2.getLength(); j++) { 324 Node child = nl2.item(j); 325 if(child.getNodeType()==Node.ELEMENT_NODE && ((Element )child).getTagName().equals("href") && child.getChildNodes().getLength()==1 && child.getFirstChild().getNodeType()==Node.TEXT_NODE) { 326 Text txt = (Text )child.getFirstChild(); 327 VirtualFile vfFile = vfs.getVirtualFile(txt.getNodeValue()).getResource(); 328 this.m_xmlResourceDisplay.setVirtualFile(vfFile); 329 } 330 } 331 } else if(element.getTagName().equals("XSL")) { 332 NodeList nl2 = element.getChildNodes(); 333 for(int j=0; j<nl2.getLength(); j++) { 334 Node child = nl2.item(j); 335 if(child.getNodeType()==Node.ELEMENT_NODE && ((Element )child).getTagName().equals("href") && child.getChildNodes().getLength()==1 && child.getFirstChild().getNodeType()==Node.TEXT_NODE) { 336 Text txt = (Text )child.getFirstChild(); 337 VirtualFile vfFile = vfs.getVirtualFile(txt.getNodeValue()).getResource(); 338 this.m_xslResourceDisplay.setVirtualFile(vfFile); 339 } 340 } 341 } 342 } 343 } 344 345 346 } catch (Exception e) { 347 e.printStackTrace(System.out); 348 } 349 } 350 351 356 public byte[] getData() { 357 byte[] data = null; 358 359 if(!this.m_bCancelled) { 360 try { 361 Document xml = 362 DocumentBuilderFactory 363 .newInstance() 364 .newDocumentBuilder() 365 .newDocument(); 366 367 Element rootEl = xml.createElement("PageDef"); 368 xml.appendChild(rootEl); 369 370 rootEl.setAttribute("timeout", ((Integer )this.m_spinner.getValue()).toString()); 371 372 Element elXML = xml.createElement("XML"); 373 rootEl.appendChild(elXML); 374 Element elHREF = xml.createElement("href"); 375 elXML.appendChild(elHREF); 376 VirtualFile vfFile = this.m_xmlResourceDisplay.getVirtualFile(); 377 if(vfFile!=null) { 378 Text txt = xml.createTextNode( vfFile.getFullPath() ); 379 elHREF.appendChild(txt); 380 } 381 382 Element elXSL = xml.createElement("XSL"); 383 rootEl.appendChild(elXSL); 384 elHREF = xml.createElement("href"); 385 elXSL.appendChild(elHREF); 386 vfFile = this.m_xslResourceDisplay.getVirtualFile(); 387 if(vfFile!=null) { 388 Text txt = xml.createTextNode( vfFile.getFullPath() ); 389 elHREF.appendChild(txt); 390 } 391 392 XMLPrettyPrint printer = new XMLPrettyPrint(); 393 printer.setNamespaceAware(false); 394 395 String sXML = printer.printNode(xml.getDocumentElement()); 396 data = sXML.getBytes(); 397 398 } catch (Exception e) { 399 e.printStackTrace(System.out); 400 } 401 } 402 403 return data; 404 } 405 406 409 private PageDefinitionDialog() throws HeadlessException { 410 super(); 411 } 412 413 417 private PageDefinitionDialog(Dialog arg0) throws HeadlessException { 418 super(arg0); 419 } 420 421 426 private PageDefinitionDialog(Dialog arg0, boolean arg1) 427 throws HeadlessException { 428 super(arg0, arg1); 429 } 430 431 435 private PageDefinitionDialog(Frame arg0) throws HeadlessException { 436 super(arg0); 437 } 438 439 444 private PageDefinitionDialog(Frame arg0, boolean arg1) 445 throws HeadlessException { 446 super(arg0, arg1); 447 } 448 449 454 private PageDefinitionDialog(Dialog arg0, String arg1) 455 throws HeadlessException { 456 super(arg0, arg1); 457 } 458 459 465 private PageDefinitionDialog(Dialog arg0, String arg1, boolean arg2) 466 throws HeadlessException { 467 super(arg0, arg1, arg2); 468 } 469 470 476 private PageDefinitionDialog(Frame arg0, String arg1, boolean arg2) 477 throws HeadlessException { 478 super(arg0, arg1, arg2); 479 } 480 481 488 private PageDefinitionDialog( 489 Dialog arg0, 490 String arg1, 491 boolean arg2, 492 GraphicsConfiguration arg3) 493 throws HeadlessException { 494 super(arg0, arg1, arg2, arg3); 495 } 496 497 503 private PageDefinitionDialog( 504 Frame arg0, 505 String arg1, 506 boolean arg2, 507 GraphicsConfiguration arg3) { 508 super(arg0, arg1, arg2, arg3); 509 } 510 511 514 public void removeLayoutComponent(Component arg0) { 515 } 516 517 520 public void addLayoutComponent(String arg0, Component arg1) { 521 } 522 523 526 public Dimension minimumLayoutSize(Container arg0) { 527 return this.getSize(); 528 } 529 530 533 public Dimension preferredLayoutSize(Container arg0) { 534 return this.getSize(); 535 } 536 537 } | Popular Tags |