gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.impl / src / main / java / org / gvsig / raster / swing / impl / openfile / OpenFileContainerImpl.java @ 1322
History | View | Annotate | Download (4.56 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.swing.impl.openfile; |
23 |
|
24 |
import java.awt.Dimension; |
25 |
import java.awt.GridBagConstraints; |
26 |
import java.awt.GridBagLayout; |
27 |
import java.io.File; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.ImageIcon; |
31 |
import javax.swing.JButton; |
32 |
import javax.swing.JPanel; |
33 |
import javax.swing.JTextField; |
34 |
|
35 |
import org.gvsig.andami.IconThemeHelper; |
36 |
import org.gvsig.gui.beans.Messages; |
37 |
import org.gvsig.raster.swing.openfile.OpenFileContainer; |
38 |
|
39 |
/**
|
40 |
* Implementation of a open file panel
|
41 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
42 |
*/
|
43 |
public class OpenFileContainerImpl extends JPanel implements OpenFileContainer { |
44 |
private static final long serialVersionUID = 5823371652872582451L; |
45 |
|
46 |
private JButton jButton = null; |
47 |
private JTextField tOpen = null; |
48 |
private OpenFileListener listener = null; |
49 |
private boolean showBorder = true; |
50 |
|
51 |
private boolean isButtonVisible = true; |
52 |
|
53 |
public OpenFileContainerImpl(boolean showBorder, String[] fileFilter, String defaultPath) { |
54 |
this.showBorder = showBorder;
|
55 |
listener = new OpenFileListener(this, fileFilter, defaultPath); |
56 |
initialize(); |
57 |
listener.setButton(this.getJButton());
|
58 |
} |
59 |
|
60 |
public OpenFileContainerImpl(boolean showBorder, List<String> fileFilter, String defaultPath) { |
61 |
this(showBorder, (String[])fileFilter.toArray(new String[0]), defaultPath); |
62 |
listener.setButton(this.getJButton());
|
63 |
} |
64 |
|
65 |
/**
|
66 |
* This method initializes this
|
67 |
*
|
68 |
*/
|
69 |
private void initialize() { |
70 |
this.setLayout(new GridBagLayout()); |
71 |
if(showBorder)
|
72 |
this.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Messages.getText("open_file"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
73 |
|
74 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
75 |
gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; |
76 |
gridBagConstraints1.weightx = 1;
|
77 |
gridBagConstraints1.insets = new java.awt.Insets(0, 2, 0, 0); |
78 |
|
79 |
this.add(getTOpen(), gridBagConstraints1);
|
80 |
|
81 |
gridBagConstraints1.gridx = 1;
|
82 |
gridBagConstraints1.weightx = 0;
|
83 |
|
84 |
this.add(getJButton(), gridBagConstraints1);
|
85 |
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* This method initializes jButton
|
90 |
*
|
91 |
* @return javax.swing.JButton
|
92 |
*/
|
93 |
private JButton getJButton() { |
94 |
if (jButton == null) { |
95 |
ImageIcon icon = null; |
96 |
try {
|
97 |
icon = IconThemeHelper.getImageIcon("icon-folder-open");
|
98 |
} catch (Exception e) { |
99 |
} |
100 |
if(icon == null || icon.getIconWidth() <= 0 || icon.getIconHeight() <= 0) |
101 |
icon = new ImageIcon(System.getProperty("user.dir") + "/src/main/resources/images/icon-folder-open.gif", ""); |
102 |
|
103 |
jButton = new JButton(icon); |
104 |
jButton.setPreferredSize(new Dimension(22, 22)); |
105 |
jButton.addActionListener(listener); |
106 |
jButton.setVisible(isButtonVisible); |
107 |
getTOpen().setEnabled(isButtonVisible); |
108 |
} |
109 |
return jButton;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* This method initializes jTextField
|
114 |
*
|
115 |
* @return javax.swing.JTextField
|
116 |
*/
|
117 |
public JTextField getTOpen() { |
118 |
if (tOpen == null) { |
119 |
tOpen = new JTextField(); |
120 |
tOpen.setPreferredSize(new Dimension(0, 22)); |
121 |
} |
122 |
return tOpen;
|
123 |
} |
124 |
|
125 |
/*
|
126 |
* (non-Javadoc)
|
127 |
* @see org.gvsig.raster.swing.openfile.OpenFileContainer#setPath(java.lang.String)
|
128 |
*/
|
129 |
public void setPath(String path) { |
130 |
getTOpen().setText(path); |
131 |
} |
132 |
|
133 |
|
134 |
/**
|
135 |
* Devuelve el file cuya ruta corresponde con el campo de texto.
|
136 |
* @return
|
137 |
*/
|
138 |
public File getFile() { |
139 |
File fil = null; |
140 |
if(this.getTOpen().getText() != "") { |
141 |
try {
|
142 |
fil = new File(this.getTOpen().getText()); |
143 |
} catch(Exception exc) { |
144 |
System.err.println("Ruta o archivo no v?lido"); |
145 |
} |
146 |
} |
147 |
return fil;
|
148 |
} |
149 |
|
150 |
public void setEnabled(boolean enabled) { |
151 |
getTOpen().setEnabled(enabled); |
152 |
getJButton().setEnabled(enabled); |
153 |
} |
154 |
|
155 |
} |