File: WorkSheetDialog.java

package info (click to toggle)
jfractionlab 0.92-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,460 kB
  • sloc: java: 10,103; sh: 182; makefile: 75
file content (413 lines) | stat: -rw-r--r-- 10,852 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package jfractionlab.jflDialogs;

import org.debian.tablelayout.TableLayout;

import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.FocusTraversalPolicy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import jfractionlab.ConfManager;
import jfractionlab.CopyDir;
import jfractionlab.JFractionLab;
import jfractionlab.officeMachine.OfficeStarter;
import lang.Messages;

public class WorkSheetDialog extends JDialog implements ActionListener{
	static final long serialVersionUID = JFractionLab.serialVersionUID;
	
	private Container content;
	private JLabel lbTitle = new JLabel("", JLabel.CENTER);
	private String title="";

	private int type;
	private String titleOptions="";
	private ButtonGroup btngrp_Options = new ButtonGroup();
	private JRadioButton rb_10 = new JRadioButton("0 - 10");
	private JRadioButton rb_100 = new JRadioButton("0 - 100");

	private String titleOptionsDouble="";
	private ButtonGroup btngrp_OptionsDouble = new ButtonGroup();
	private JRadioButton rb_double_10 = new JRadioButton("0 - 10");
	private JRadioButton rb_double_100 = new JRadioButton("0 - 100");

	private String templatePath="";
	private JTextField jtfTemplatePath = new JTextField();
	private JButton btnFC = new JButton(" ... ");
	
	private JButton btnOK = new JButton();
	private JButton btnCancel = new JButton();
	private double sizes_main[][];
	private MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy();

	private int nb_of_optionFields;
	private File selectedFile;
	
	//ONLY_FILE_CHOOSER
	public WorkSheetDialog(int type, String title){
		super();
		this.nb_of_optionFields = 0;
		myConstructor(type,title, "", "");
	}
	
	//ONE_OPTIONS_FIELD
	public WorkSheetDialog(int type, String title, String titleOptions){
		super();
		this.nb_of_optionFields = 1;
		myConstructor(type,title,titleOptions,"");
	}
	
	//TWO_OPTIONS_FILED
	public WorkSheetDialog(
			int type,
			String title,
			String titleOptions,
			String titleOptionsDouble
	){
		super();
		this.nb_of_optionFields = 2;
		myConstructor(type,title,titleOptions,titleOptionsDouble);
	}
	
	public void myConstructor(
			int type,
			String title,
			String titleOptions,
			String titleOptionsDouble
	){
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		this.type = type;
		this.title = lang.Messages.getString("worksheets")+" : "+title;
		if(!titleOptions.equals("")){
			this.titleOptions = lang.Messages.getString("range_of_numbers")+" : "+titleOptions;
		}
		if(!titleOptionsDouble.equals("")){
			this.titleOptionsDouble = lang.Messages.getString("range_of_numbers")+" : "+titleOptionsDouble;
		}
		createGUI();
	}
	
	private void createGUI(){
		setTitle(lang.Messages.getString("generate_worksheets"));
		if(nb_of_optionFields == 0){
			sizes_main = new double[][] {{
				5,
				TableLayout.FILL,
				TableLayout.FILL,
				5
			},{
				5,
				0.20,
				TableLayout.FILL,
				0.20,
				5
			}};  //Spalten / Zeilen
		}else if(nb_of_optionFields == 1){
			sizes_main = new double[][] {{
				5,
				TableLayout.FILL,
				TableLayout.FILL,
				5
			},{
				5,
				0.20,
				TableLayout.FILL,
				TableLayout.FILL,
				0.20,
				5
			}};  //Spalten / Zeilen
		}else if(nb_of_optionFields == 2){
			sizes_main = new double[][] {{
				5,
				TableLayout.FILL,
				TableLayout.FILL,
				5
			},{
				5,
				0.15,
				TableLayout.FILL,
				TableLayout.FILL,
				TableLayout.FILL,
				0.15,
				5
			}}; //Spalten / Zeilen
		}
		getRootPane().setDefaultButton(btnOK);
		content = getContentPane();
		content.setLayout(new TableLayout(sizes_main));
		lbTitle.setText(title);
		content.add(lbTitle,"1,1,2,1");
		
		if(nb_of_optionFields == 0){
			content.add(createFileChooser(),"1,2,2,2");
		}else if(nb_of_optionFields == 1){
			content.add(createOptions(false), "1,2,2,2");
			content.add(createFileChooser(),"1,3,2,3");
		}else if(nb_of_optionFields == 2){
			content.add(createOptions(false), "1,2,2,2");
			content.add(createOptions(true), "1,3,2,3");
			content.add(createFileChooser(),"1,4,2,4");
		}
		
		btnCancel.setText(lang.Messages.getString("cancel"));
		btnCancel.addActionListener(this);
		btnOK.setText(lang.Messages.getString("OK"));
		btnOK.addActionListener(this);

		if(nb_of_optionFields == 0){
			content.add(btnCancel, "1,3,c,c");
			content.add(btnOK, "2,3,c,c");
		}else if(nb_of_optionFields == 1){
			content.add(btnCancel, "1,4,c,c");
			content.add(btnOK, "2,4,c,c");
		}else if(nb_of_optionFields == 2){
			content.add(btnCancel, "1,5,c,c");
			content.add(btnOK, "2,5,c,c");
		}
		
		
		content.setFocusTraversalPolicy(newPolicy);
		setLocation(50, 150);
		
		if(nb_of_optionFields == 0){
			setSize(500,210);
		}else if(nb_of_optionFields == 1){
			setSize(500,280);
		}else if(nb_of_optionFields == 2){
			setSize(500,350);
		}

		setResizable(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
	}

	private JPanel createOptions(boolean isDouble){
		double sizes_options[][];
		sizes_options = new double[][] {{
			5,
			TableLayout.FILL,
			TableLayout.FILL,
			5
		},{
			5,
			TableLayout.FILL,
			TableLayout.FILL,
			TableLayout.FILL,
			5
		}}; //Spalten / Zeilen
		JPanel pnOptions = new JPanel();
		pnOptions.setLayout(new TableLayout(sizes_options));
		pnOptions.setBorder(BorderFactory.createEtchedBorder());
		if(isDouble==false){
			JLabel lbOptionsTitle = new JLabel(titleOptions, JLabel.CENTER);
			pnOptions.add(lbOptionsTitle, "1,1");
			pnOptions.add(rb_10, "1,2");
			pnOptions.add(rb_100, "1,3");
			btngrp_Options.add(rb_10);
			btngrp_Options.add(rb_100);
			rb_10.setSelected(true);
		}else{
			JLabel lbOptionsTitle = new JLabel(titleOptionsDouble, JLabel.CENTER);
			pnOptions.add(lbOptionsTitle, "1,1");
			pnOptions.add(rb_double_10, "1,2");
			pnOptions.add(rb_double_100, "1,3");
			btngrp_OptionsDouble.add(rb_double_10);
			btngrp_OptionsDouble.add(rb_double_100);
			rb_double_10.setSelected(true);
		}
		return pnOptions;
	}
	
	private JPanel createFileChooser(){
		double sizes_fc[][];
		sizes_fc = new double[][] {{
			5,
			0.85,
			TableLayout.FILL,
			5
		},{
			5,
			30,
			TableLayout.FILL,
			5
		}}; //Spalten / Zeilen
		JPanel pnFC = new JPanel();
		pnFC.setLayout(new TableLayout(sizes_fc));
		pnFC.setBorder(BorderFactory.createEtchedBorder());
		
		pnFC.add(new JLabel(lang.Messages.getString("get_odt_template")),"1,1,2,1");
		pnFC.add(jtfTemplatePath, "1,2");
		btnFC.addActionListener(this);
		pnFC.add(btnFC,"2,2");
		return pnFC;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if(obj == btnFC){
			fcAction();
		}else if (obj == btnCancel){
				close();
		}else if(obj == btnOK){
			if(templatePath.equals("")){
				JOptionPane.showMessageDialog(null, lang.Messages.getString("get_odt_template"));
			}else{
				okAction();
				close();
			}
		}
	}

	public void okAction(){
		int max1 = 10;
		int max2 = 10;
		
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		
		if(nb_of_optionFields > 0){
			if(rb_10.isSelected()){
				max1 = 10;
			}else{
				max1 = 100;
			}
			System.out.println("max1 = "+max1);
		}
		if(nb_of_optionFields == 2){
			if(rb_double_10.isSelected()){
				max2 = 10;
			}else{
				max2= 100;
			}
			System.out.println("max2 = "+max2);
		}
		new OfficeStarter(
				this,
				selectedFile,
				type,
				max1,
				max2
		);
		close();
	}
	
	private void fcAction(){
		JFileChooser fc = new JFileChooser();
		FileFilter myfilter = new FileNameExtensionFilter("odt", "ODT");
		fc.setFileFilter(myfilter);
		fc.setLocale(lang.Messages.getLocale());
		
		String str_templatedirectory = new ConfManager().getConfdir()+System.getProperty("file.separator")+"templates";
		File templatedirectory = new File(str_templatedirectory);

		//FIXME neue oder geaenderte vorlagen kopieren
		if(!(templatedirectory.exists())){
			File srcdir = new File("");
			boolean bl = false;
			if(new File("/usr/share/jfractionlab/templates/").exists()){
				srcdir = new File("/usr/share/jfractionlab/templates/");
				bl = true;
			}else if(new File("C:\\Programme\\JFractionLab\\templates").exists()){
				srcdir = new File("C:\\Programme\\JFractionLab\\templates");
				bl = true;
			}else{
				JOptionPane.showMessageDialog(null, Messages.getString("find_the_templates")+"\n"+str_templatedirectory);
			}
//			System.out.println("srcdir = "+srcdir.toString());
			if(bl){
				try {
					CopyDir.copyDir(srcdir , new File(str_templatedirectory));
				} catch (IOException e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, "Can not copy the files from\n"+
							srcdir.toString()+" to "+str_templatedirectory+"\n"+
							"This should not happen. Please send a bug report to gnugeo@gnugeo.de\n" +
							"Thanks !!"
							);
					templatedirectory = srcdir;
				}
			}
		}
		
		fc.setCurrentDirectory(templatedirectory);
		
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			templatePath = fc.getSelectedFile().toString();
			this.selectedFile = fc.getSelectedFile();
		}else{
			templatePath = "";
		}
		jtfTemplatePath.setText(templatePath);
	}
	
	private void close(){
		setVisible(false);
		dispose();
	}
	
	public class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy {
		@Override
		public Component getComponentAfter(Container aContainer,Component aComponent) {
			//soll: btn_yes, btn_no,
			if(aComponent.equals(jtfTemplatePath)) {
				return btnFC;
			}else if(aComponent.equals(btnFC)) {
				return btnCancel;
			}else if(aComponent.equals(btnCancel)){
				return btnOK;
			}else if(aComponent.equals(btnOK)) {
				return btnCancel;
			}else{
				return btnOK;
			}
		}

		@Override
		public Component getComponentBefore(Container aContainer,Component aComponent) {
			//soll: btn_yes, btn_no
			if(aComponent.equals(btnOK)) {
				return btnCancel;
			}else if(aComponent.equals(btnCancel)){
				return btnFC;
			}else if(aComponent.equals(btnFC)){
				return jtfTemplatePath;
			}else{
				return jtfTemplatePath;
			}
		}

		@Override
		public Component getDefaultComponent(Container aContainer) {
			return btnOK;
		}

		@Override
		public Component getFirstComponent(Container aContainer) {
			return jtfTemplatePath;
		}

		@Override
		public Component getLastComponent(Container aContainer) {return null;}
	}//MyOwnFocusTraversalPolicy
}