NewFileDialog.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00036 package gui;
00037 
00038 import java.lang.*;    // Java language classes 
00039 import java.awt.*;     // User Interface components
00040 import java.io.*;      // IO specific classes (File, Stream,..)
00041 import java.awt.event.*;   // Event handling
00042 
00043 public class NewFileDialog extends Dialog
00044 {
00045   // Class variables ..
00046 
00047   public static final int LOAD = 0;
00048   public static final int SAVE = 1;
00049 
00050   // Instance variables ..
00051 
00052   protected TextField directoryField;
00053   protected TextField fileField;
00054   protected TextField filterField;
00055   protected List fileList;
00056 
00057   protected String directory = null;
00058   protected String file = null;
00059   protected int mode = -1;
00060 
00061   // Instance creation
00062 
00063   public NewFileDialog(Frame dw, String title)
00064   {
00065     super(dw, title, true);
00066 
00067     GridBagLayout gridbag = new GridBagLayout();
00068     GridBagConstraints constr = new GridBagConstraints();
00069     setLayout(gridbag);
00070 
00071     directoryField = new TextField(40);
00072     directoryField.addTextListener(new TextListener()
00073                            { public void textValueChanged(TextEvent e)
00074                              {directoryChanged();}});
00075  
00076     Panel p1 = new Panel();
00077     p1.add(new Label("Directory"));
00078     p1.add(directoryField);
00079     constr.fill = GridBagConstraints.NONE;
00080     constr.gridwidth = GridBagConstraints.REMAINDER;
00081     gridbag.setConstraints(p1,constr);
00082     add(p1);
00083 
00084     fileList = new List(4);
00085     fileList.addItemListener(new ItemListener()
00086                       { public void itemStateChanged(ItemEvent e) {
00087                         itemSelected();}});
00088     constr.fill = GridBagConstraints.BOTH;
00089     constr.gridwidth = GridBagConstraints.REMAINDER;
00090     constr.weighty = 1.0;
00091     gridbag.setConstraints(fileList,constr);
00092     add(fileList);
00093 
00094     Panel psum = new Panel();
00095 
00096     filterField = new TextField(8);
00097     Panel p4 = new Panel();
00098     p4.add(new Label("Filter"));
00099     p4.add(filterField);
00100     psum.add(p4);
00101 
00102     fileField = new TextField(20);
00103     Panel p3 = new Panel();
00104     p3.add(new Label("File"));
00105     p3.add(fileField);
00106     psum.add(p3);
00107     constr.fill = GridBagConstraints.NONE;
00108     constr.gridwidth = GridBagConstraints.REMAINDER;
00109     constr.weighty = 0.0;
00110     gridbag.setConstraints(psum,constr);
00111     add(psum);
00112 
00113     // Create buttons
00114 
00115     Panel p5 = new Panel();
00116     Button okButton = new Button("Ok");
00117     okButton.addActionListener(new ActionListener()
00118                                { public void actionPerformed(ActionEvent e)
00119                                    { okPressed();}});
00120     Button cancelButton = new Button("Cancel");
00121     cancelButton.addActionListener(new ActionListener()
00122                                    { public void 
00123                                        actionPerformed(ActionEvent e)
00124                                        { cancelPressed();}});
00125     p5.add(okButton);
00126     p5.add(cancelButton);
00127     constr.fill = GridBagConstraints.NONE;
00128     constr.gridwidth = GridBagConstraints.REMAINDER;
00129     gridbag.setConstraints(p5,constr);
00130     add(p5);
00131 
00132     // Initialize this dialog to its preferred size
00133 
00134     setSize(580,200);
00135     pack();
00136   }
00137 
00138   // Action handling
00139 
00140   public void okPressed()
00141   {
00142     directory = directoryField.getText();
00143     file = fileField.getText();
00144     closeDialog();
00145   }
00146 
00147   public void cancelPressed()
00148   {
00149     directory = null;
00150     file = null;
00151     closeDialog();
00152   }
00153 
00154   public void closeDialog()
00155   {
00156     setVisible(false);
00157     dispose();
00158   }
00159 
00160   public void directoryChanged() {
00161     File dir = new File(directoryField.getText());
00162     if (dir.isDirectory()) {
00163       loadDirectory(dir);
00164     }
00165   }
00166 
00167   public void loadDirectory(File dir) {
00168     String[] dirlist = dir.list();
00169     int i;
00170     if (fileList.getItemCount()>0) {
00171       fileList.removeAll();
00172     }
00173     fileList.add("..");
00174     fileList.addItemListener(new ItemListener()
00175                     { public void itemStateChanged(ItemEvent e) {
00176                         itemSelected();}});
00177     for(i=0;i<dirlist.length;i++) {
00178       fileList.add(dirlist[i]);
00179     }
00180   }
00181 
00182   public void itemSelected() {
00183     if (fileList.getSelectedIndex() >= 0) {
00184       String item = fileList.getSelectedItem();
00185       fileList.deselect(fileList.getSelectedIndex());
00186       File fileOrDir;
00187       File newDir;
00188       if (item.equals("..")) {
00189         fileOrDir = new File(directoryField.getText());
00190         String dirStr = fileOrDir.getParent();
00191         if (dirStr != null) {
00192           newDir = new File(dirStr);
00193           directoryField.setText(newDir.getAbsolutePath());
00194           directoryChanged();
00195         }
00196       } else {
00197         fileOrDir = new File(directoryField.getText(),item);
00198         if (fileOrDir.isDirectory()) {
00199           directoryField.setText(fileOrDir.getAbsolutePath());
00200           directoryChanged();
00201         } else {
00202           fileField.setText(item);
00203         }
00204       }  
00205     }
00206   }
00207 
00208   // Accessing
00209 
00210   public synchronized void setDirectory(String dir) {
00211     File fdir = new File(dir);
00212     if (!fdir.isDirectory()) {
00213       fdir = new File("");
00214     }
00215     directoryField.setText(fdir.getAbsolutePath());
00216     loadDirectory(fdir);
00217   }
00218 
00219   public String getDirectory() {
00220     return directory;
00221   }
00222 
00223   public synchronized void setFile(String file) {
00224     fileField.setText(file);
00225   }
00226 
00227   public String getFile() {
00228     return file;
00229   }
00230 
00231   public synchronized void setFilenameFilter(FilenameFilter filter) {
00232   }
00233 
00234   public FilenameFilter getFilenameFilter() {
00235     return null;
00236   }
00237 
00238   public synchronized void setMode(int mode) {
00239     this.mode = mode;
00240   }
00241 
00242   public int getMode(int mode) {
00243     return mode;
00244   }
00245 
00246 }
00247 


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16