Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / Test.java @ 2165

History | View | Annotate | Download (7.13 KB)

1
package org.gvsig.tools.swing.impl.pickercontroller;
2

    
3
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
4
import org.gvsig.tools.swing.api.ToolsSwingLocator;
5
import org.gvsig.tools.swing.api.windowmanager.Dialog;
6
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
7
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
8
import com.jgoodies.forms.layout.CellConstraints;
9
import com.jgoodies.forms.layout.FormLayout;
10
import java.awt.BorderLayout;
11
import java.awt.ComponentOrientation;
12
import java.awt.Container;
13
import java.awt.Dimension;
14
import java.awt.event.ActionEvent;
15
import java.awt.event.ActionListener;
16
import javax.swing.Box;
17
import javax.swing.ImageIcon;
18
import javax.swing.JButton;
19
import javax.swing.JComponent;
20
import javax.swing.JFormattedTextField;
21
import javax.swing.JLabel;
22
import javax.swing.JPanel;
23
import javax.swing.JTextField;
24
import org.gvsig.tools.swing.api.Component;
25

    
26
/**
27
 *
28
 * @author jjdelcerro
29
 */
30
public class Test {
31

    
32
  public static class TestPanelView extends JPanel {
33

    
34
    JTextField txtFile1 = new JTextField();
35
    JButton btnFile1 = new JButton();
36
    JTextField txtColor1 = new JTextField();
37
    JButton btnColor1 = new JButton();
38
    JFormattedTextField txtDate = new JFormattedTextField();
39
    JButton btnDate = new JButton();
40

    
41
    /**
42
     * Default constructor
43
     */
44
    public TestPanelView() {
45
      initializePanel();
46
    }
47

    
48
    /**
49
     * Adds fill components to empty cells in the first row and first column of
50
     * the grid. This ensures that the grid spacing will be the same as shown in
51
     * the designer.
52
     *
53
     * @param cols an array of column indices in the first row where fill
54
     * components should be added.
55
     * @param rows an array of row indices in the first column where fill
56
     * components should be added.
57
     */
58
    void addFillComponents(Container panel, int[] cols, int[] rows) {
59
      Dimension filler = new Dimension(10, 10);
60

    
61
      boolean filled_cell_11 = false;
62
      CellConstraints cc = new CellConstraints();
63
      if (cols.length > 0 && rows.length > 0) {
64
        if (cols[0] == 1 && rows[0] == 1) {
65
          /**
66
           * add a rigid area
67
           */
68
          panel.add(Box.createRigidArea(filler), cc.xy(1, 1));
69
          filled_cell_11 = true;
70
        }
71
      }
72

    
73
      for (int index = 0; index < cols.length; index++) {
74
        if (cols[index] == 1 && filled_cell_11) {
75
          continue;
76
        }
77
        panel.add(Box.createRigidArea(filler), cc.xy(cols[index], 1));
78
      }
79

    
80
      for (int index = 0; index < rows.length; index++) {
81
        if (rows[index] == 1 && filled_cell_11) {
82
          continue;
83
        }
84
        panel.add(Box.createRigidArea(filler), cc.xy(1, rows[index]));
85
      }
86

    
87
    }
88

    
89
    /**
90
     * Helper method to load an image file from the CLASSPATH
91
     *
92
     * @param imageName the package and name of the file to load relative to the
93
     * CLASSPATH
94
     * @return an ImageIcon instance with the specified image file
95
     * @throws IllegalArgumentException if the image resource cannot be loaded.
96
     */
97
    public ImageIcon loadImage(String imageName) {
98
      try {
99
        ClassLoader classloader = getClass().getClassLoader();
100
        java.net.URL url = classloader.getResource(imageName);
101
        if (url != null) {
102
          ImageIcon icon = new ImageIcon(url);
103
          return icon;
104
        }
105
      } catch (Exception e) {
106
        e.printStackTrace();
107
      }
108
      throw new IllegalArgumentException("Unable to load image: " + imageName);
109
    }
110

    
111
    /**
112
     * Method for recalculating the component orientation for right-to-left
113
     * Locales.
114
     *
115
     * @param orientation the component orientation to be applied
116
     */
117
    public void applyComponentOrientation(ComponentOrientation orientation) {
118
      // Not yet implemented...
119
      // I18NUtils.applyComponentOrientation(this, orientation);
120
      super.applyComponentOrientation(orientation);
121
    }
122

    
123
    public JPanel createPanel() {
124
      JPanel jpanel1 = new JPanel();
125
      FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE", "CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
126
      CellConstraints cc = new CellConstraints();
127
      jpanel1.setLayout(formlayout1);
128

    
129
      JLabel jlabel1 = new JLabel();
130
      jlabel1.setText("Fichero");
131
      jpanel1.add(jlabel1, cc.xy(3, 2));
132

    
133
      txtFile1.setName("txtFile1");
134
      jpanel1.add(txtFile1, cc.xy(5, 2));
135

    
136
      btnFile1.setActionCommand("...");
137
      btnFile1.setName("btnFile1");
138
      btnFile1.setText("...");
139
      jpanel1.add(btnFile1, cc.xy(7, 2));
140

    
141
      JLabel jlabel2 = new JLabel();
142
      jlabel2.setText("Color");
143
      jpanel1.add(jlabel2, cc.xy(3, 4));
144

    
145
      txtColor1.setName("txtColor1");
146
      jpanel1.add(txtColor1, cc.xy(5, 4));
147

    
148
      btnColor1.setActionCommand("...");
149
      btnColor1.setName("btnColor1");
150
      btnColor1.setText("...");
151
      jpanel1.add(btnColor1, cc.xy(7, 4));
152

    
153
      JLabel jlabel3 = new JLabel();
154
      jlabel3.setText("Fecha");
155
      jpanel1.add(jlabel3, cc.xy(3, 6));
156

    
157
      txtDate.setName("txtDate");
158
      jpanel1.add(txtDate, cc.xy(5, 6));
159

    
160
      btnDate.setActionCommand("...");
161
      btnDate.setName("btnDate");
162
      btnDate.setText("...");
163
      jpanel1.add(btnDate, cc.xy(7, 6));
164

    
165
      addFillComponents(jpanel1, new int[]{1, 2, 3, 4, 5, 6, 7, 8}, new int[]{1, 2, 3, 4, 5, 6, 7});
166
      return jpanel1;
167
    }
168

    
169
    /**
170
     * Initializer
171
     */
172
    protected void initializePanel() {
173
      setLayout(new BorderLayout());
174
      add(createPanel(), BorderLayout.CENTER);
175
    }
176

    
177
  }
178

    
179
  private static class TestPanel 
180
          extends TestPanelView 
181
          implements Component
182
    {
183

    
184
    private DatePickerControllerImpl pickerDate;
185
    private FilePickerControllerImpl pickerFile;
186
    private ColorPickerControllerImpl pickerColor;
187

    
188
    public TestPanel() {
189
      initComponents();
190
    }
191
    
192
    @Override
193
    public JComponent asJComponent() {
194
      return this;
195
    }
196
    
197
    private void initComponents() {
198
      this.pickerDate = new DatePickerControllerImpl(txtDate, btnDate);
199
      this.pickerFile = new FilePickerControllerImpl(txtFile1, btnFile1);
200
      this.pickerColor = new ColorPickerControllerImpl(txtColor1, btnColor1);
201
      
202
      this.setPreferredSize(new Dimension(300, 200));
203
    }
204
  }
205
  /**
206
   * @param args the command line arguments
207
   */
208
  public static void main(String[] args) throws Exception {
209
    Test test = new Test();
210
    new DefaultLibrariesInitializer().fullInitialize();
211
    test.run();
212
  }
213

    
214
  public void run() throws Exception {
215
    WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
216

    
217
    TestPanel panel = new TestPanel();
218
    Dialog dialog = windowManager.createDialog(
219
            panel.asJComponent(),
220
            "Test",
221
            null,
222
            WindowManager_v2.BUTTONS_OK_CANCEL
223
    );
224
    dialog.addActionListener(new ActionListener() {
225
      @Override
226
      public void actionPerformed(ActionEvent e) {
227
        System.out.println("Date:"+panel.pickerDate.get());
228
      }
229
    });
230
    dialog.show(WindowManager.MODE.WINDOW);
231
  }
232

    
233
}