1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.idltree; 26 27 import java.io.*; 28 import java.util.*; 29 import javax.swing.*; 30 import javax.swing.event.*; 31 import javax.swing.tree.*; 32 import java.awt.*; 33 import java.awt.event.*; 34 35 public class IdlEditor { 36 public static void display(String title, IdlNode root) { 37 try { 38 IdlModel model = new IdlModel(root); 39 IdlTreePanel panel = new IdlTreePanel(model); 40 final JFrame frame = new JFrame(); 41 frame.setTitle(title); 42 frame.getContentPane().add(panel); 43 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 44 frame.addWindowListener(new WindowAdapter() { 45 public void windowClosed(WindowEvent e) { 46 frame.dispose(); 47 } 48 }); 49 frame.pack(); 50 frame.setVisible(true); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 } 55 56 59 60 73 public static boolean edit(String title, IdlNode root) { 74 try { 75 Lock lock = new Lock(); 76 final JFrame frame = new JFrame(); 77 frame.setTitle(title); 78 final EditPanel panel = new EditPanel(lock, root); 79 frame.getContentPane().add(panel); 80 frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 81 frame.addWindowListener(new WindowAdapter() { 82 public void windowClosed(WindowEvent e) { 83 panel.release(); 84 frame.removeWindowListener(this); 85 frame.dispose(); 86 } 87 }); 88 frame.pack(); 89 frame.setVisible(true); 90 91 lock.getLock(); 93 frame.setVisible(false); 94 frame.dispose(); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 98 return true; 99 } 100 101 public static class EditPanel extends JPanel { 102 private Lock lock; 103 104 public EditPanel(Lock lock, IdlNode root) { 105 this.lock = lock; 106 setLayout(new BorderLayout()); 107 IdlModel model = new IdlModel(root); 108 IdlTreePanel panel = new IdlTreePanel(model); 109 JButton okButton = new JButton("ok"); 110 okButton.addActionListener(new java.awt.event.ActionListener () { 111 public void actionPerformed(ActionEvent e) { 112 okButton_actionPerformed(e); 113 } 114 }); 115 add(okButton, BorderLayout.SOUTH); 116 add(panel, BorderLayout.CENTER); 117 } 118 119 void okButton_actionPerformed(ActionEvent e) { 120 lock.release(); 121 } 122 123 public void release() { 124 lock.release(); 125 } 126 } 127 128 static class Lock { 129 private boolean free; 130 131 public synchronized void getLock() { 132 while(!free) { 133 try { 134 wait(); 135 } catch (Exception e) { 136 } 137 } 138 } 139 140 public synchronized void release() { 141 free = true; 142 notifyAll(); 143 } 144 } 145 } 146 | Popular Tags |