KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > miniinstaller > gui > InputPanel


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.miniinstaller.gui;
27
28 import java.awt.*;
29 import java.io.*;
30 import javax.swing.*;
31 import java.awt.event.*;
32 import net.yagga.util.Ut;
33
34 public class InputPanel extends JPanel implements ActionListener,
35   InstallPanel, KeyListener{
36
37   //JLabel lbTitle = new JLabel();
38
JTextArea taText = new JTextArea();
39   TitledBorderBean tit1=new TitledBorderBean();
40
41   String JavaDoc title;
42   String JavaDoc text;
43   String JavaDoc defVal;
44   boolean browse;
45   boolean fileOrDir;
46   String JavaDoc inputValue="";
47   CanFwdListener canFwdListener;
48   JPanel jPanel1 = new JPanel();
49   TitledBorderBean titledBorderBean1 = new TitledBorderBean();
50   JButton btBrowse = new JButton();
51   JTextField tfInput = new JTextField();
52   GridBagLayout gridBagLayout1 = new GridBagLayout();
53   GridBagLayout gridBagLayout2 = new GridBagLayout();
54   JLabel lbTitle = new JLabel();
55
56   public InputPanel(String JavaDoc title, String JavaDoc text, String JavaDoc defVal, boolean fileOrText, boolean fileOrDir, CanFwdListener cfl) {
57     canFwdListener=cfl;
58     this.title=title;
59     this.text=text;
60     this.browse=fileOrText;
61     this.fileOrDir=fileOrDir;
62     this.defVal=defVal;
63
64     try {
65       jbInit();
66     }
67     catch (Exception JavaDoc ex) {
68       ex.printStackTrace();
69     }
70   }
71
72   void jbInit() throws Exception JavaDoc {
73     taText.setText(text);
74     taText.setEditable(false);
75     taText.setWrapStyleWord(true);
76     taText.setLineWrap(true);
77     taText.setOpaque(false);
78         taText.setFont(GuiProperties.textFont);
79     this.setBackground(SystemColor.control);
80     //this.setOpaque(false);
81
//btBrowse.setText("jButton1");
82
if(!browse)
83       btBrowse.setVisible(false);
84     btBrowse.setOpaque(false);
85     titledBorderBean1.setTitle(title);
86         titledBorderBean1.setTitleFont(GuiProperties.makeItalic(GuiProperties.textFont));
87     //titledBorderBean1.setOpaque(false);
88
btBrowse.addActionListener(this);
89     btBrowse.setText("Browse...");
90         btBrowse.setForeground(InstallFrame.BTN_COL);
91     tfInput.setText(defVal);
92     tfInput.addKeyListener(this);
93     tfInput.setColumns(5);
94     //tfInput.setText("tfInput");
95
this.setLayout(gridBagLayout2);
96     jPanel1.setLayout(gridBagLayout1);
97     jPanel1.setOpaque(false);
98
99     lbTitle.setText(title);
100         lbTitle.setFont(GuiProperties.smallTitleFont);
101     this.add(taText, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
102             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(8, 7, 8, 7), 0, 170));
103     this.add(jPanel1, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0
104             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(8, 8, 8, 8), 0, 0));
105     jPanel1.add(tfInput, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0
106             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 2, 5), 0, 0));
107     jPanel1.add(btBrowse, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
108             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 0, 2, 5), 0, 0));
109     tit1.setTitle("test");
110     javax.swing.border.EtchedBorder JavaDoc eBorder=new javax.swing.border.EtchedBorder JavaDoc();
111     titledBorderBean1.setBorder(eBorder);
112     jPanel1.setBorder(titledBorderBean1);
113     this.add(lbTitle, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
114             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
115     refresh();
116   }
117
118   public void refresh(){
119     this.setBackground(InstallFrame.BK_COL);
120     jPanel1.setBackground(InstallFrame.BK_COL);
121     this.setMaximumSize(InstallFrame.panelDim);
122     this.setPreferredSize(InstallFrame.panelDim);
123     this.setMinimumSize(InstallFrame.panelDim);
124     taText.setForeground(InstallFrame.FG_COL);
125     titledBorderBean1.setTitleColor(InstallFrame.FG_COL);
126         lbTitle.setForeground(InstallFrame.FG_COL);
127
128     //check in case we have a default value
129
this.checkEnable();
130         this.invalidate();
131   }
132
133
134   String JavaDoc getValue(){
135     //trim trailing "\" if any...
136
String JavaDoc f=tfInput.getText();
137     if(browse && (f.endsWith("\\") || f.endsWith("/")))
138       return f.substring(0,f.length()-1);
139
140     return f;
141   }
142
143   public void actionPerformed(ActionEvent e){
144
145     if(e.getSource()==btBrowse){
146       JFileChooser jfc;
147       if(defVal.equals(""))
148         jfc=new JFileChooser(".");
149       else
150         jfc=new JFileChooser(defVal);
151
152       if(fileOrDir)
153         jfc.setFileSelectionMode(jfc.FILES_ONLY);
154       else
155         jfc.setFileSelectionMode(jfc.DIRECTORIES_ONLY);
156
157             //sets colors
158
jfc.setBackground(InstallFrame.BK_COL);
159             jfc.setOpaque(true);
160             InstallFrame.deepSet(jfc,false);
161
162       int returnVal=jfc.showOpenDialog(this);
163       try{
164       if(returnVal==JFileChooser.APPROVE_OPTION)
165         inputValue=(jfc.getSelectedFile().getCanonicalPath());
166         tfInput.setText(inputValue);
167         checkEnable();
168       }catch(IOException ioe){
169       }
170     }
171   }
172
173   /**
174     KEY LISTENER
175   */

176   public void keyPressed(KeyEvent e){
177     checkEnable();
178   }
179   public void keyReleased(KeyEvent e){
180     checkEnable();
181   }
182   public void keyTyped(KeyEvent e){
183     checkEnable();
184   }
185
186   private void checkEnable(){
187     //Ut.infoln("text '"+tfInput.getText()+"':len="+tfInput.getText().length());
188
if(tfInput.getText().length()>0)
189       canFwdListener.canFwd();
190   }
191
192
193 }
194
195
Popular Tags