5 // Created by Lutz Mueller on 5/11/07.
8 // Copyright (C) 2007 Lutz Mueller
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 import java
.awt
.color
.*;
27 import java
.awt
.event
.*;
32 @SuppressWarnings("unchecked")
33 public class WindowFrame
extends gsObject
{
36 String frameCloseAction
= null;
37 String frameMoveAction
= null;
38 String frameResizeAction
= null;
41 public WindowFrame(StringTokenizer params
)
43 jframe
= new JFrame();
45 id
= params
.nextToken();
46 container
= jframe
.getContentPane();
47 component
= container
;
49 int x
= Integer
.parseInt(params
.nextToken());
50 int y
= Integer
.parseInt(params
.nextToken());
51 int width
= Integer
.parseInt(params
.nextToken());
52 int height
= Integer
.parseInt(params
.nextToken());
54 gsObject
.widgets
.put(id
, this);
56 jframe
.setBounds(x
, y
, width
, height
);
58 // not visible on OSX, but on Win32 and Linux
59 jframe
.setIconImage(Toolkit
.getDefaultToolkit().getImage(getClass().getResource("/images/newLISP32.png")));
61 if(params
.hasMoreTokens())
62 jframe
.setTitle(Base64Coder
.decodeString(params
.nextToken()));
64 if(params
.hasMoreTokens())
66 if(params
.nextToken().equals("true"))
67 jframe
.setVisible(true);
71 WindowAdapter listener
= new WindowAdapter() {
72 public void windowClosing(WindowEvent w
)
74 if(frameCloseAction
!= null)
76 jframe
.setDefaultCloseOperation(JFrame
.DO_NOTHING_ON_CLOSE
);
77 guiserver
.out
.println("(" + frameCloseAction
+ "\"" + id
+ "\")");
78 guiserver
.out
.flush();
83 jframe
.addWindowListener(listener
);
86 ComponentAdapter componentListener
= new ComponentAdapter() {
87 public void componentResized(ComponentEvent re
)
89 if(frameResizeAction
!= null)
91 guiserver
.out
.println("(" + frameResizeAction
+ "\"" + id
+ "\"" +
92 jframe
.getWidth() + " " + jframe
.getHeight() +")");
93 guiserver
.out
.flush();
97 public void componentMoved(ComponentEvent me
)
99 if(frameMoveAction
!= null)
101 guiserver
.out
.println("(" + frameMoveAction
+ "\"" + id
+ "\"" +
102 jframe
.getLocationOnScreen().x
+ " " + jframe
.getLocationOnScreen().y
+")");
103 guiserver
.out
.flush();
108 jframe
.addComponentListener(componentListener
);
110 jframe
.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
114 public void setVisible(StringTokenizer tokens
)
117 if(tokens
.nextToken().equals("true"))
118 jframe
.setVisible(true);
120 jframe
.setVisible(false);
124 public void setText(StringTokenizer tokens
)
126 String text
= Base64Coder
.decodeString(tokens
.nextToken());
127 jframe
.setTitle(text
);
131 public void getText(StringTokenizer tokens
)
133 String action
= tokens
.nextToken();
134 String text
= jframe
.getTitle();
135 if(text
.length() == 0)
136 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\")");
138 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" [text]" + Base64Coder
.encodeString(text
) + "[/text])");
139 guiserver
.out
.flush();
143 public void clearText(StringTokenizer tokens
)
149 public void setResizable(StringTokenizer tokens
)
151 if(tokens
.nextToken().equals("true"))
152 jframe
.setResizable(true);
154 jframe
.setResizable(false);
158 public void confirmDialog(StringTokenizer tokens
)
160 String action
= tokens
.nextToken();
161 String title
= Base64Coder
.decodeString(tokens
.nextToken());
162 String message
= Base64Coder
.decodeString(tokens
.nextToken());
163 int option
= JOptionPane
.YES_NO_OPTION
;
165 if(tokens
.hasMoreTokens())
167 if(!tokens
.nextToken().equals("yes-no"))
168 option
= JOptionPane
.YES_NO_CANCEL_OPTION
;
171 int result
= JOptionPane
.showConfirmDialog(jframe
, message
, title
, option
);
173 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" " + result
+ ")");
174 guiserver
.out
.flush();
178 public void messageDialog(StringTokenizer tokens
)
180 String title
= Base64Coder
.decodeString(tokens
.nextToken());
181 String message
= Base64Coder
.decodeString(tokens
.nextToken());
182 int option
= JOptionPane
.PLAIN_MESSAGE
;
185 if(tokens
.hasMoreTokens())
187 String soption
= tokens
.nextToken();
188 if(soption
.equals("error")) option
= JOptionPane
.ERROR_MESSAGE
;
189 if(soption
.equals("information")) option
= JOptionPane
.INFORMATION_MESSAGE
;
190 if(soption
.equals("warning")) option
= JOptionPane
.WARNING_MESSAGE
;
191 if(soption
.equals("question")) option
= JOptionPane
.QUESTION_MESSAGE
;
194 if(tokens
.hasMoreTokens())
196 path
= Base64Coder
.decodeString(tokens
.nextToken());
197 JOptionPane
.showMessageDialog(jframe
, message
, title
, option
,
198 guiserver
.getIconFromPath(path
, this.getClass()));
201 JOptionPane
.showMessageDialog(jframe
, message
, title
, option
);
205 public void ColorChooser(StringTokenizer tokens
)
208 String action
= tokens
.nextToken();
209 String title
= Base64Coder
.decodeString(tokens
.nextToken());
214 if(tokens
.hasMoreTokens()) // get initial color
216 red
= Float
.parseFloat(tokens
.nextToken());
217 green
= Float
.parseFloat(tokens
.nextToken());
218 blue
= Float
.parseFloat(tokens
.nextToken());
219 color
= new Color(red
, green
, blue
);
221 else color
= Color
.white
;
223 color
= JColorChooser
.showDialog(jframe
, title
, color
);
227 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"color\")");
228 guiserver
.out
.flush();
232 red
= (new Integer(color
.getRed())).floatValue();
233 green
= (new Integer(color
.getGreen())).floatValue();
234 blue
= (new Integer(color
.getBlue())).floatValue();
240 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"color\" " + red
+ " " + green
+ " " + blue
+ ")");
241 guiserver
.out
.flush();
244 public void OpenFileChooser(StringTokenizer tokens
)
246 JFileChooser filechooser
;
249 String action
= tokens
.nextToken();
251 if(tokens
.hasMoreTokens())
252 filechooser
= new JFileChooser(Base64Coder
.decodeString(tokens
.nextToken()));
254 filechooser
= new JFileChooser();
256 filechooser
.setFileSelectionMode(JFileChooser
.FILES_AND_DIRECTORIES
);
258 if(tokens
.hasMoreTokens())
260 MyFileFilter myfilter
= new MyFileFilter();
261 myfilter
.extensions
= Base64Coder
.decodeString(tokens
.nextToken());
262 myfilter
.description
= Base64Coder
.decodeString(tokens
.nextToken());
264 filechooser
.setFileFilter(myfilter
);
267 int selected
= filechooser
.showOpenDialog(container
);
269 if(selected
== JFileChooser
.APPROVE_OPTION
)
271 file
= Base64Coder
.encodeString(filechooser
.getSelectedFile().getAbsolutePath());
272 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"open\" " + "\"" + file
+ "\")");
276 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"open\")");
279 guiserver
.out
.flush();
283 public void SaveFileChooser(StringTokenizer tokens
)
285 JFileChooser filechooser
;
287 String initialFile
= null;
289 String action
= tokens
.nextToken();
291 if(tokens
.hasMoreTokens()) // initial directory
292 filechooser
= new JFileChooser(Base64Coder
.decodeString(tokens
.nextToken()));
294 filechooser
= new JFileChooser();
296 if(tokens
.hasMoreTokens()) // initial file name
298 initialFile
= Base64Coder
.decodeString(tokens
.nextToken());
299 filechooser
.setSelectedFile(new File(initialFile
));
303 if(tokens
.hasMoreTokens())
305 MyFileFilter myfilter
= new MyFileFilter();
306 myfilter
.extensions
= Base64Coder
.decodeString(tokens
.nextToken());
307 myfilter
.description
= Base64Coder
.decodeString(tokens
.nextToken());
308 filechooser
.setFileFilter(myfilter
);
311 int selected
= filechooser
.showSaveDialog(container
);
313 if(selected
== JFileChooser
.APPROVE_OPTION
)
315 file
= Base64Coder
.encodeString(filechooser
.getSelectedFile().getAbsolutePath());
316 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"save\" " + "\"" + file
+ "\")");
319 guiserver
.out
.println("(" + action
+ " \"" + id
+ "\" \"save\")");
321 guiserver
.out
.flush();
324 public void setFont(StringTokenizer tokens
)
326 ErrorDialog
.wrongApplication(Dispatcher
.cmd
, id
);
329 public void setToolTip(StringTokenizer tokens
)
331 ErrorDialog
.wrongApplication(Dispatcher
.cmd
, id
);
334 public void dispose(StringTokenizer tokens
)
339 public void setPreferredSize(StringTokenizer tokens
)
341 int width
= Integer
.parseInt(tokens
.nextToken());
342 int height
= Integer
.parseInt(tokens
.nextToken());
343 jframe
.setSize(new Dimension(width
, height
));
346 public void setBackground(StringTokenizer tokens
)
348 Float red
= Float
.parseFloat(tokens
.nextToken());
349 Float green
= Float
.parseFloat(tokens
.nextToken());
350 Float blue
= Float
.parseFloat(tokens
.nextToken());
351 if(tokens
.hasMoreTokens())
353 Float alpha
= Float
.parseFloat(tokens
.nextToken());
354 jframe
.setBackground(new Color(red
, green
, blue
, alpha
));
357 jframe
.setBackground(new Color(red
, green
, blue
));
360 public void requestFocus(StringTokenizer tokens
)
362 jframe
.requestFocus();
365 public void frameCloseEvent(StringTokenizer tokens
)
367 frameCloseAction
= tokens
.nextToken();
370 public void frameResizeEvent(StringTokenizer tokens
)
372 frameResizeAction
= tokens
.nextToken();
375 public void frameMoveEvent(StringTokenizer tokens
)
377 frameMoveAction
= tokens
.nextToken();
380 public void getBounds(StringTokenizer tokens
)
382 int x
, y
, width
, height
;
384 x
= jframe
.getLocationOnScreen().x
;
385 y
= jframe
.getLocationOnScreen().y
;
387 width
= jframe
.getWidth();
388 height
= jframe
.getHeight();
390 guiserver
.out
.println("( set 'gs:bounds '(" + x
+ " " + y
+ " " + width
+ " " + height
+ "))\n");
391 guiserver
.out
.flush();