KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > IdlEditor


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
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 JavaDoc 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 JavaDoc e) {
52             e.printStackTrace();
53         }
54     }
55
56     /**
57      * return true if the value has been modified
58      */

59
60 /* public static boolean edit(String title, IdlNode root) {
61         try {
62             AnyModel model = new AnyModel(root);
63             AnyTreePanel panel = new AnyTreePanel(model);
64             if (JOptionPane.showConfirmDialog(null, panel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.CANCEL_OPTION) {
65                 return false;
66             }
67         } catch (Exception e) {
68             e.printStackTrace();
69         }
70         return true;
71     }
72 */

73     public static boolean edit(String JavaDoc 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             // wait until the ok button is pressed
92
lock.getLock();
93             frame.setVisible(false);
94             frame.dispose();
95         } catch (Exception JavaDoc 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 JavaDoc() {
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 JavaDoc e) {
136                 }
137             }
138         }
139
140         public synchronized void release() {
141             free = true;
142             notifyAll();
143         }
144     }
145 }
146
Popular Tags