VisClient/org/hfbk/vis/VisClientWindow.java

Go to the documentation of this file.
00001 /*
00002  * Created on 27.04.2008
00003  *
00004  */
00005 package org.hfbk.vis;
00006 
00007 import java.awt.BorderLayout;
00008 import java.awt.Component;
00009 import java.awt.Dimension;
00010 import java.awt.FlowLayout;
00011 import java.awt.Frame;
00012 import java.awt.Graphics;
00013 import java.awt.Image;
00014 import java.awt.Label;
00015 import java.awt.MenuItem;
00016 import java.awt.MouseInfo;
00017 import java.awt.Panel;
00018 import java.awt.Point;
00019 import java.awt.PopupMenu;
00020 import java.awt.TextField;
00021 import java.awt.event.ActionEvent;
00022 import java.awt.event.ActionListener;
00023 import java.awt.event.KeyAdapter;
00024 import java.awt.event.KeyEvent;
00025 import java.awt.event.MouseAdapter;
00026 import java.awt.event.MouseEvent;
00027 import java.awt.event.MouseMotionAdapter;
00028 import java.awt.event.WindowAdapter;
00029 import java.awt.event.WindowEvent;
00030 import java.io.File;
00031 import java.io.FilenameFilter;
00032 
00033 import org.hfbk.util.Sleeper;
00034 import org.hfbk.util.Tooltip;
00035 import org.hfbk.util.UIUtils;
00036 import org.lwjgl.LWJGLException;
00037 
00046 public class VisClientWindow extends Frame {
00047 
00048         VisClientPanel clientPanel = new VisClientPanel();
00049 
00051         //PopupMenu sourcePopup = new PopupMenu();
00052 
00053         public VisClientWindow() throws LWJGLException {
00054                 super("Vis / Client");
00055 
00056                 setUndecorated(true);
00057                 setIconImage(ImageLoader.getRessourceImage("icons/vis.png"));
00058                 addWindowListener(new WindowAdapter() {
00059                         public void windowClosing(WindowEvent e) {
00060                                 dispose();
00061                         }
00062                 });
00063 
00064                 setLayout(new BorderLayout());
00065                 add(clientPanel);
00066                 add(buildUI(), BorderLayout.SOUTH);
00067                 
00068                 UIUtils.blackify(this);
00069                 pack();         
00070                 setVisible(true);
00071                 clientPanel.requestFocus();
00072         }
00073 
00074         PopupMenu buildSourcesMenu(final TextField sourceField) {
00075                 PopupMenu sourcePopup=new PopupMenu();
00076                 ActionListener sourceItemListener = new ActionListener() {
00077                         public void actionPerformed(ActionEvent e) {
00078                                 sourceField.setText(e.getActionCommand());
00079                                 clientPanel.requestFocus();
00080                         }
00081                 };
00082                 for (final String source : Prefs.current.sources.split(",")) {
00083                         MenuItem itm = new MenuItem(source);
00084                         itm.addActionListener(sourceItemListener);
00085                         sourcePopup.add(itm);
00086                 }
00087                 MenuItem itm = new MenuItem("Edit...");
00088                 itm.addActionListener(new ActionListener() {
00089                         public void actionPerformed(ActionEvent e) {
00090                                 new PrefsDialog("sources");
00091                         }
00092                 });
00093                 sourcePopup.add(itm);
00094                 return sourcePopup;
00095         }
00096 
00097         PopupMenu buildScriptsMenu() {          
00098                 PopupMenu menu=new PopupMenu();
00099                 
00100                 ActionListener scriptItemListener = new ActionListener() {
00101                         public void actionPerformed(ActionEvent e) {
00102                                 String script = e.getActionCommand();
00103                                 VisClientScripter scripter = new VisClientScripter(
00104                                                 clientPanel.client);
00105                                 scripter.source("scripts/" + script);
00106                                 clientPanel.requestFocus();
00107                         }
00108                 };
00109                 String[] files = new File("scripts").list(new FilenameFilter() {
00110                         public boolean accept(File dir, String name) {
00111                                 return name.endsWith(".vs");
00112                         }
00113                 });
00114                 for (final String script : files) {
00115                         MenuItem itm = new MenuItem(script);
00116                         itm.addActionListener(scriptItemListener);
00117                         menu.add(itm);
00118                 }               
00119                 return menu; 
00120         }
00121 
00133         public Panel buildrequester()
00134         {
00135                 Panel requester=new Panel();
00136                 
00137                 requester.add(new Label("request"));
00138 
00139                 
00140                 return requester;
00141         }
00142         
00143         public Panel buildUI() {
00144 
00145                 Panel bar = new Panel();
00146                 bar.setLayout(new BorderLayout());
00147 
00148                 Panel p = new Panel();
00149                 p.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
00150 
00151                 // the source field has a menu where default sources
00152                 // can be chosen and added.
00153                 final TextField sourceField = new TextField("google");
00154                 
00155                 sourceField.addMouseListener(new MouseAdapter() {
00156                         public void mouseClicked(MouseEvent e) {
00157                                 if (e.getButton() == MouseEvent.BUTTON1){
00158                                         PopupMenu menu=buildSourcesMenu(sourceField);
00159                                         sourceField.add(menu);
00160                                         menu.show(sourceField, e.getX(), e.getY());
00161                                         sourceField.requestFocus();
00162                                 }
00163                         }
00164                 });
00165                 p.add(new Label("Source:"));
00166                 p.add(sourceField);
00167 
00168                 final TextField keywordField = new TextField(20); // url query parts
00169                 keywordField.addActionListener(new ActionListener() {
00170                         public void actionPerformed(ActionEvent e) {
00171                                 String source = sourceField.getText(), keyword = keywordField
00172                                                 .getText();
00173                                 
00174                                 clientPanel.client.root.fetch(source, keyword, null);
00175 
00176                                 keywordField.setText("");
00177                                 clientPanel.requestFocus(); // put focus to client canvas
00178                                 // to enable keyboard control. it is automatically
00179                                 // put back if letters are entered.
00180                         };
00181                 });
00182                 p.add(new Label("Keyword:"));
00183                 sourceField.addActionListener(new ActionListener(){
00184                         public void actionPerformed(ActionEvent arg0) {
00185                                 keywordField.requestFocus();
00186                         }
00187                 });
00188                 p.add(keywordField);
00189         
00190                 // we use a own key listener on the client canvas
00191                 // to steal its keypresses if it is not ctrl+something.
00192                 // then we conveniently switch the focus to the keyword.
00193                 clientPanel.addKeyListener(new KeyAdapter() {
00194                         public void keyTyped(KeyEvent e) {
00195                                 if (Prefs.current.autoFocus && !e.isControlDown() && !Character.isISOControl(e.getKeyChar())) {
00196                                         //= '\b'
00197                                         
00198                                         keywordField.requestFocus();
00199                                         // as the first char is just consumed, put it "by hand"
00200                                         keywordField.setText(keywordField.getText()
00201                                                         + e.getKeyChar());
00202                                         keywordField.setCaretPosition(keywordField.getText()
00203                                                         .length());
00204                                 }
00205                         }
00206                 });
00207 
00208                 try {
00209 
00210                         p.add(PrefsDialog.getPrefsCheckbox(Prefs.current.getClass()
00211                                         .getField("log")));
00212 
00213                         p.add(new Label("log as:"));
00214                         p.add(PrefsDialog.getPrefsStringInput(Prefs.current.getClass()
00215                                         .getField("user"), 10));
00216 
00217                         p.add(PrefsDialog.getPrefsCheckbox(Prefs.current.getClass()
00218                                         .getField("osk")));
00219                         p.add(new Label("OSK"));
00220 
00221                         p.add(PrefsDialog.getPrefsCheckbox(Prefs.current.getClass()
00222                                         .getField("map")));
00223                         p.add(new Label("map"));
00224                 } catch (Exception e1) {
00225                         throw new RuntimeException(e1);
00226                 }
00227 
00228                 // enough ui so far, place it as main ui bar element.
00229                 bar.add(p, BorderLayout.CENTER);
00230 
00231                 // some right aligned buttons for window manipulation
00232                 p = new Panel();
00233 
00234                 IconButton helpButton = new IconButton("icons/help.png","quick help");
00235                 helpButton.addMouseListener(new MouseAdapter() {
00236                         public void mouseClicked(MouseEvent e) {
00237                                 new VisClientScripter(clientPanel.client).source("scripts/help.vs");
00238                         }
00239                 });
00240                 p.add(helpButton);
00241 
00242                 IconButton scriptButton = new IconButton("icons/scripts.png",
00243                                 "run scripts");
00244                 scriptButton.addMouseListener(new MouseAdapter() {
00245                         public void mouseClicked(MouseEvent e) {
00246                                 PopupMenu menu=buildScriptsMenu();
00247                                 e.getComponent().add(menu);
00248                                 menu.show(e.getComponent(), e.getX(), e.getY());
00249                                 sourceField.requestFocus();
00250                         }
00251                 });
00252                 p.add(scriptButton);
00253 
00254                 IconButton prefsButton = new IconButton("icons/prefs.png",
00255                                 "open preferences dialog");
00256                 prefsButton.addActionListener(new ActionListener() {
00257                         public void actionPerformed(ActionEvent e) {
00258                                 new PrefsDialog();
00259                         }
00260                 });
00261                 p.add(prefsButton);
00262 
00263                 IconButton closeButton = new IconButton("icons/close.png", "close");
00264                 closeButton.addActionListener(new ActionListener() {
00265                         public void actionPerformed(ActionEvent e) {
00266                                 dispose();
00267                         }
00268                 });
00269                 p.add(closeButton);
00270 
00271                 IconButton iconifyButton = new IconButton("icons/iconify.png",
00272                                 "move window");
00273                 iconifyButton.addActionListener(new ActionListener() {
00274                         public void actionPerformed(ActionEvent e) {
00275                                 setExtendedState(Frame.ICONIFIED);
00276                         }
00277                 });
00278                 p.add(iconifyButton);
00279 
00280                 final IconButton moveButton = new IconButton("icons/move.png",
00281                                 "move window");
00282                 moveButton.addMouseMotionListener(new MouseMotionAdapter() {
00283                         public void mouseDragged(MouseEvent e) {
00284                                 int x = e.getX(), y = e.getY();
00285                                 Point loc = getLocation();
00286                                 setLocation(loc.x + x, loc.y + y);
00287                         }
00288                 });
00289                 p.add(moveButton);
00290 
00291                 final IconButton fullButton = new IconButton("icons/resize.png",
00292                                 "toggle fullscreen or drag to resize");
00293                 fullButton.addActionListener(new ActionListener() {
00294                         public void actionPerformed(ActionEvent e) {
00295                                 if (getExtendedState() == Frame.MAXIMIZED_BOTH) {
00296                                         setExtendedState(Frame.NORMAL);
00297                                         moveButton.setVisible(true);
00298                                 } else {
00299                                         setExtendedState(Frame.MAXIMIZED_BOTH);
00300                                         moveButton.setVisible(false);
00301                                 }
00302                         }
00303                 });
00304                 fullButton.addMouseMotionListener(new MouseMotionAdapter() {
00305                         public void mouseDragged(MouseEvent e) {
00306                                 // as this button itselfs lags while layouting,
00307                                 // use screen space coordinates to prevent oscilation.
00308                                 Point wPos = getLocationOnScreen();
00309                                 Point mPos = MouseInfo.getPointerInfo().getLocation();
00310                                 setSize(mPos.x - wPos.x + 10, mPos.y - wPos.y + 10);
00311                                 validate();
00312                         }
00313                 });
00314                 p.add(fullButton);
00315 
00316                 bar.add(p, BorderLayout.EAST);
00317 
00318                 return bar;
00319         }
00320 
00322         class IconButton extends Component {
00323                 Image image;
00324 
00325                 ActionListener al;
00326 
00327                 IconButton(String iconfile, String tooltip) {
00328 
00329                         new Tooltip(tooltip, this);
00330                         //setToolTipText(tooltip);
00331                         
00332                         if (iconfile != null)
00333                                 image = ImageLoader.getRessourceImage(iconfile);
00334 
00335                         addMouseListener(new MouseAdapter() {
00336                                 public void mouseClicked(MouseEvent e) {
00337                                         if (al != null)
00338                                                 al.actionPerformed(new ActionEvent(this,
00339                                                                 ActionEvent.ACTION_PERFORMED, null));
00340                                 }
00341                         });
00342                 }
00343 
00344                 public Dimension getPreferredSize() {
00345                         return new Dimension(image.getWidth(null), image.getHeight(null));
00346                 }
00347 
00348                 void addActionListener(ActionListener a) {
00349                         al = a;
00350                 }
00351 
00352                 public void paint(Graphics g) {
00353                         g.drawImage(image, 0, 0, null);
00354                 }
00355         }
00356         
00357         public void dispose() {
00358                 setVisible(false);
00359                 remove(clientPanel);
00360                 clientPanel.client.reset();
00361                 clientPanel=null;
00362                 
00363                 Sleeper.sleep(500); //wait a little to let threads come down 
00364                 super.dispose();
00365                 System.exit(0);
00366         }
00367 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1