2323import java .awt .GridBagLayout ;
2424import java .awt .Insets ;
2525import java .awt .event .ActionEvent ;
26+ import java .io .File ;
27+ import java .io .IOException ;
2628
29+ import javax .imageio .ImageIO ;
2730import javax .swing .JButton ;
2831import javax .swing .JDialog ;
2932import javax .swing .JFileChooser ;
3235import javax .swing .JPanel ;
3336import javax .swing .JTextField ;
3437import javax .swing .border .EmptyBorder ;
38+ import javax .swing .filechooser .FileNameExtensionFilter ;
39+
40+ import net .sourceforge .dionysus .Category ;
3541
3642/**
3743 * A dialog box to edit the categories
@@ -43,11 +49,13 @@ public class NewCategoryDialog extends JDialog {
4349 /** The UID for serialization */
4450 private static final long serialVersionUID = -3243725927349670922L ;
4551
52+ private Category category ;
53+
4654 /** Widget for category name */
47- private JTextField nameField ;
55+ private final JTextField nameField ;
4856
4957 /** Widget for category image */
50- private JTextField imageField ;
58+ private final JTextField imageField ;
5159
5260 /** UI holder */
5361 private final JPanel contentPanel ;
@@ -60,6 +68,9 @@ public class NewCategoryDialog extends JDialog {
6068 */
6169 public NewCategoryDialog () {
6270 fc = new JFileChooser ();
71+ fc .setMultiSelectionEnabled (false );
72+ fc .setFileFilter (new FileNameExtensionFilter (Messages .getString ("NewCategoryDialog.4" ), //$NON-NLS-1$
73+ ImageIO .getReaderFileSuffixes ()));
6374 contentPanel = new JPanel ();
6475 setModal (true );
6576 setLocationRelativeTo (null );
@@ -73,80 +84,86 @@ public NewCategoryDialog() {
7384 gbl_contentPanel .columnWeights = new double [] { 0.0 , 1.0 , 1.0 , Double .MIN_VALUE };
7485 gbl_contentPanel .rowWeights = new double [] { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , Double .MIN_VALUE };
7586 contentPanel .setLayout (gbl_contentPanel );
76- {
77- final JLabel lblNewLabel = new JLabel (Messages .getString ("NewCategoryDialog.1" )); //$NON-NLS-1$
78- final GridBagConstraints gbc_lblNewLabel = new GridBagConstraints ();
79- gbc_lblNewLabel .fill = GridBagConstraints .HORIZONTAL ;
80- gbc_lblNewLabel .insets = new Insets (0 , 0 , 5 , 5 );
81- gbc_lblNewLabel .anchor = GridBagConstraints .LINE_END ;
82- gbc_lblNewLabel .gridx = 1 ;
83- gbc_lblNewLabel .gridy = 0 ;
84- contentPanel .add (lblNewLabel , gbc_lblNewLabel );
85- }
86- {
87- nameField = new JTextField ();
88- final GridBagConstraints gbc_nameField = new GridBagConstraints ();
89- gbc_nameField .insets = new Insets (0 , 0 , 5 , 0 );
90- gbc_nameField .fill = GridBagConstraints .HORIZONTAL ;
91- gbc_nameField .gridx = 2 ;
92- gbc_nameField .gridy = 0 ;
93- contentPanel .add (nameField , gbc_nameField );
94- nameField .setColumns (10 );
95- }
96- {
97- final JLabel lblNewLabel_1 = new JLabel (Messages .getString ("NewCategoryDialog.2" )); //$NON-NLS-1$
98- final GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints ();
99- gbc_lblNewLabel_1 .fill = GridBagConstraints .HORIZONTAL ;
100- gbc_lblNewLabel_1 .anchor = GridBagConstraints .LINE_END ;
101- gbc_lblNewLabel_1 .insets = new Insets (0 , 0 , 5 , 5 );
102- gbc_lblNewLabel_1 .gridx = 1 ;
103- gbc_lblNewLabel_1 .gridy = 1 ;
104- contentPanel .add (lblNewLabel_1 , gbc_lblNewLabel_1 );
105- }
106- {
107- imageField = new JTextField ();
108- final GridBagConstraints gbc_imageField = new GridBagConstraints ();
109- gbc_imageField .insets = new Insets (0 , 0 , 5 , 0 );
110- gbc_imageField .fill = GridBagConstraints .HORIZONTAL ;
111- gbc_imageField .gridx = 2 ;
112- gbc_imageField .gridy = 1 ;
113- contentPanel .add (imageField , gbc_imageField );
114- imageField .setColumns (10 );
115- }
116- {
117- final JButton btnImage = new JButton (Messages .getString ("NewCategoryDialog.3" )); //$NON-NLS-1$
118- final GridBagConstraints gbc_btnImage = new GridBagConstraints ();
119- gbc_btnImage .insets = new Insets (0 , 0 , 5 , 0 );
120- gbc_btnImage .gridx = 3 ;
121- gbc_btnImage .gridy = 1 ;
122- btnImage .addActionListener (e -> {
123- final int returnVal = fc .showOpenDialog (contentPanel );
124- if (returnVal == JFileChooser .APPROVE_OPTION ) {
125- // TODO: check image and initialize the corresponding field
126- }
127- });
128- contentPanel .add (btnImage , gbc_btnImage );
129- }
130- {
131- final JPanel buttonPane = new JPanel ();
132- buttonPane .setLayout (new FlowLayout (FlowLayout .RIGHT ));
133- getContentPane ().add (buttonPane , BorderLayout .SOUTH );
134- {
135- final JButton okButton = new JButton (Messages .getString ("NewCategoryDialog.5" ), Constants .ok ); //$NON-NLS-1$
136- okButton .setActionCommand (Messages .getString ("NewCategoryDialog.6" )); //$NON-NLS-1$
137-
138- okButton .addActionListener (this ::onOK );
139-
140- buttonPane .add (okButton );
141- getRootPane ().setDefaultButton (okButton );
142- }
143- {
144- final JButton cancelButton = new JButton (Messages .getString ("NewCategoryDialog.11" ), Constants .cancel ); //$NON-NLS-1$
145- cancelButton .setActionCommand (Messages .getString ("NewCategoryDialog.12" )); //$NON-NLS-1$
14687
147- cancelButton .addActionListener (arg0 -> setVisible (false ));
88+ final JLabel lblNewLabel = new JLabel (Messages .getString ("NewCategoryDialog.1" )); //$NON-NLS-1$
89+ final GridBagConstraints gbc_lblNewLabel = new GridBagConstraints ();
90+ gbc_lblNewLabel .fill = GridBagConstraints .HORIZONTAL ;
91+ gbc_lblNewLabel .insets = new Insets (0 , 0 , 5 , 5 );
92+ gbc_lblNewLabel .anchor = GridBagConstraints .LINE_END ;
93+ gbc_lblNewLabel .gridx = 1 ;
94+ gbc_lblNewLabel .gridy = 0 ;
95+ contentPanel .add (lblNewLabel , gbc_lblNewLabel );
96+
97+ nameField = new JTextField ();
98+ final GridBagConstraints gbc_nameField = new GridBagConstraints ();
99+ gbc_nameField .insets = new Insets (0 , 0 , 5 , 0 );
100+ gbc_nameField .fill = GridBagConstraints .HORIZONTAL ;
101+ gbc_nameField .gridx = 2 ;
102+ gbc_nameField .gridy = 0 ;
103+ contentPanel .add (nameField , gbc_nameField );
104+ nameField .setColumns (10 );
105+
106+ final JLabel lblNewLabel_1 = new JLabel (Messages .getString ("NewCategoryDialog.2" )); //$NON-NLS-1$
107+ final GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints ();
108+ gbc_lblNewLabel_1 .fill = GridBagConstraints .HORIZONTAL ;
109+ gbc_lblNewLabel_1 .anchor = GridBagConstraints .LINE_END ;
110+ gbc_lblNewLabel_1 .insets = new Insets (0 , 0 , 5 , 5 );
111+ gbc_lblNewLabel_1 .gridx = 1 ;
112+ gbc_lblNewLabel_1 .gridy = 1 ;
113+ contentPanel .add (lblNewLabel_1 , gbc_lblNewLabel_1 );
114+
115+ imageField = new JTextField ();
116+ final GridBagConstraints gbc_imageField = new GridBagConstraints ();
117+ gbc_imageField .insets = new Insets (0 , 0 , 5 , 0 );
118+ gbc_imageField .fill = GridBagConstraints .HORIZONTAL ;
119+ gbc_imageField .gridx = 2 ;
120+ gbc_imageField .gridy = 1 ;
121+ contentPanel .add (imageField , gbc_imageField );
122+ imageField .setColumns (10 );
123+
124+ final JButton btnImage = new JButton (Messages .getString ("NewCategoryDialog.3" )); //$NON-NLS-1$
125+ final GridBagConstraints gbc_btnImage = new GridBagConstraints ();
126+ gbc_btnImage .insets = new Insets (0 , 0 , 5 , 0 );
127+ gbc_btnImage .gridx = 3 ;
128+ gbc_btnImage .gridy = 1 ;
129+ btnImage .addActionListener (this ::onChooseImage );
130+ contentPanel .add (btnImage , gbc_btnImage );
131+
132+ final JPanel buttonPane = new JPanel ();
133+ buttonPane .setLayout (new FlowLayout (FlowLayout .RIGHT ));
134+ getContentPane ().add (buttonPane , BorderLayout .SOUTH );
135+
136+ final JButton okButton = new JButton (Messages .getString ("NewCategoryDialog.5" ), Constants .ok ); //$NON-NLS-1$
137+ okButton .setActionCommand (Messages .getString ("NewCategoryDialog.6" )); //$NON-NLS-1$
138+
139+ okButton .addActionListener (this ::onOK );
140+
141+ buttonPane .add (okButton );
142+ getRootPane ().setDefaultButton (okButton );
143+
144+ final JButton cancelButton = new JButton (Messages .getString ("NewCategoryDialog.11" ), Constants .cancel ); //$NON-NLS-1$
145+ cancelButton .setActionCommand (Messages .getString ("NewCategoryDialog.12" )); //$NON-NLS-1$
146+
147+ cancelButton .addActionListener (arg0 -> setVisible (false ));
148+
149+ buttonPane .add (cancelButton );
150+
151+ }
148152
149- buttonPane .add (cancelButton );
153+ /**
154+ *
155+ */
156+ private void onChooseImage (ActionEvent e ) {
157+ final int returnVal = fc .showOpenDialog (contentPanel );
158+ if (returnVal == JFileChooser .APPROVE_OPTION ) {
159+ // Try to populate the UI widget with the path to the selected file
160+ final File img = fc .getSelectedFile ();
161+ try {
162+ imageField .setText (img .getCanonicalPath ());
163+ } catch (final IOException e1 ) {
164+ JOptionPane .showMessageDialog (null , Messages .getString ("NewCategoryDialog.10" ), //$NON-NLS-1$
165+ Messages .getString ("NewCategoryDialog.9" ), //$NON-NLS-1$
166+ JOptionPane .WARNING_MESSAGE );
150167 }
151168 }
152169 }
@@ -159,8 +176,6 @@ private void onOK(ActionEvent evt) {
159176 if (nameField .getText ().isEmpty ()) {
160177 throw new IllegalArgumentException (Messages .getString ("NewCategoryDialog.7" )); //$NON-NLS-1$
161178 }
162- } catch (final NumberFormatException e ) {
163- e .printStackTrace ();
164179 } catch (final IllegalArgumentException e ) {
165180 JOptionPane .showMessageDialog (null , Messages .getString ("NewCategoryDialog.8" ) + e .getMessage (), //$NON-NLS-1$
166181 Messages .getString ("NewCategoryDialog.9" ), //$NON-NLS-1$
0 commit comments