summaryrefslogtreecommitdiff
path: root/MyFntSel.java
blob: 0f371df1e9ac0487a88d5843c94a3133c874a425 (plain)
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
/*
 * MyFntSel.java - very basic font selector
 *
 * Copyright (c) 2025 Stefano Marchetti
 *
 * This file is part of Jedecma - breast ultrasound examinations archiving software
 *
 * Jedecma is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jedecma is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jedecma.  If not, see <http://www.gnu.org/licenses/>.
 *
*/


package jedecma;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class MyFntSel extends JDialog {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JButton okBut, defBut;
	private String curFontSt, defFontSt, selFontSt;
	private int curSize, curStyle, selSize, selStyle, defSize, defStyle;
	private static final String okString = Jedecma.localMessagesBundle
			.getString("CB_OK");
	private static final String defString = "DEFAULT";
	String[] fonts;
	String[] sizes = { "8", "9", "10", "11", "12", "13", "14", "15", "16",
			"17", "18", "19", "20" };
	String[] styles = { "PLAIN", "BOLD", "ITALIC" };
	int[] styleId = { Font.PLAIN, Font.BOLD, Font.ITALIC };
	JList fontList, sizeList, styleList;
	MyJlabel test;
	boolean selected;

	MyFntSel() {
		super(Jedecma.mf, "Font", true); // E' un JDialog modale!
		defFontSt = "Dialog";
		defStyle = Font.PLAIN;
		defSize = 12;

		selFontSt = "";
		selSize = 0;
		selStyle = 0;

		curFontSt = "";
		curSize = 0;
		curStyle = 0;

		sizeList = new JList(sizes);
		styleList = new JList(styles);
		// ricava lista dei fontdisponibili
		GraphicsEnvironment e = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		// Font[] fonts = e.getAllFonts();
		fonts = e.getAvailableFontFamilyNames();
		fontList = new JList(fonts);

		selected = false;
	}

	String getSelFont() {
		String s = "";
		if (selected) {
			s += selFontSt;
			switch (selStyle) {
			case Font.PLAIN: {
				s += ",PLAIN";
			}
				break;
			case Font.BOLD: {
				s += ",BOLD";
			}
				break;
			case Font.ITALIC: {
				s += ",ITALIC";
			}
				break;
			}
			s += "," + String.valueOf(selSize);
		}
		return s;
	}

	public void chooseFont(String title, String fnt, String def) {
		// legge font default
		if (!def.equals("")) {
			Font font = Uti1.myFont(def);
			if (font != null) {
				defFontSt = font.getName();
				defSize = font.getSize();
				defStyle = font.getStyle();
			}
		}

		// legge font corrente
		if (!fnt.equals("")) {
			Font font = Uti1.myFont(fnt);
			if (font != null) {
				curFontSt = font.getName();
				curSize = font.getSize();
				curStyle = font.getStyle();
			}
		}

		if (curFontSt.equals("")) {
			curFontSt = defFontSt;
			curSize = defSize;
			curStyle = defStyle;
		}

		// String testString = "The Quick Silver Fox Jumped Over The Lazy Dog";
		String testString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";

		JPanel mainPan = new JPanel();
		mainPan.setLayout(new BoxLayout(mainPan, BoxLayout.Y_AXIS));

		JPanel fontPan = new JPanel();
		fontPan.setLayout(new BoxLayout(fontPan, BoxLayout.X_AXIS));
		fontPan.setBorder(BorderFactory.createTitledBorder(""));

		JScrollPane fontNamesPan = new JScrollPane(fontList);
		fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		fontNamesPan.setBorder(BorderFactory.createTitledBorder("Font"));
		updateLists();

		{
			MouseListener mouseListener = new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent mouseEvent) {
					JList theList = (JList) mouseEvent.getSource();
					if (mouseEvent.getClickCount() == 1) {
						int index = theList.locationToIndex(mouseEvent
								.getPoint());
						if (index >= 0) {
							Object o = theList.getModel().getElementAt(index);
							String s = o.toString();
							setCurrent(s, curSize, curStyle);
							refreshGui();
						}
					}
				}
			};
			fontList.addMouseListener(mouseListener);
		}
		fontPan.add(fontNamesPan);

		JScrollPane fontSizePan = new JScrollPane(sizeList);
		fontSizePan.setBorder(BorderFactory.createTitledBorder("Dim"));
		updateLists();

		{
			MouseListener mouseListener = new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent mouseEvent) {
					JList theList = (JList) mouseEvent.getSource();
					if (mouseEvent.getClickCount() == 1) {
						int index = theList.locationToIndex(mouseEvent
								.getPoint());
						if (index >= 0) {
							Object o = theList.getModel().getElementAt(index);
							int size = 0;
							size = Integer.parseInt(o.toString());
							setCurrent(curFontSt, size, curStyle);
							refreshGui();
						}
					}
				}
			};
			sizeList.addMouseListener(mouseListener);
		}
		fontPan.add(fontSizePan);

		JScrollPane fontAttrPan = new JScrollPane(styleList);
		fontAttrPan.setBorder(BorderFactory.createTitledBorder("Attrib"));
		updateLists();

		{
			MouseListener mouseListener = new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent mouseEvent) {
					JList theList = (JList) mouseEvent.getSource();
					if (mouseEvent.getClickCount() == 1) {
						int index = theList.locationToIndex(mouseEvent
								.getPoint());
						if (index >= 0) {
							Object o = theList.getModel().getElementAt(index);
							int style = 0;
							if (o.toString().toUpperCase().equals("PLAIN")) {
								style = Font.PLAIN;
							} else if (o.toString().toUpperCase()
									.equals("BOLD")) {
								style = Font.BOLD;
							} else if (o.toString().toUpperCase().equals(
									"ITALIC")) {
								style = Font.ITALIC;
							}
							setCurrent(curFontSt, curSize, style);
							refreshGui();
						}
					}
				}
			};
			styleList.addMouseListener(mouseListener);
		}
		fontPan.add(fontAttrPan);

		mainPan.add(fontPan);

		JPanel testPan = new JPanel();
		testPan.setBorder(BorderFactory.createTitledBorder("Test"));
		test = new MyJlabel(testString);
		testPan.add(test);
		mainPan.add(testPan);

		JPanel butPan = new JPanel();

		okBut = new JButton(okString);
		okBut.setMnemonic(KeyEvent.VK_K);
		okBut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okBut.setEnabled(false);
				selFontSt = curFontSt;
				selStyle = curStyle;
				selSize = curSize;
				selected = true;
				stop();
			}
		});
		// okBut.setActionCommand(okString);
		okBut.setEnabled(true);
		butPan.add(okBut);

		defBut = new JButton(defString);
		defBut.setMnemonic(KeyEvent.VK_K);
		defBut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				defBut.setEnabled(false);
				// updateLists();
				setCurrent(defFontSt, defSize, defStyle);
				refreshGui();
				defBut.setEnabled(true);
			}
		});
		// defBut.setActionCommand(okString);
		defBut.setEnabled(true);
		butPan.add(defBut);

		mainPan.add(butPan);

		getContentPane().add(mainPan);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// quello che deve essere fatto alla chiusura della finestra
			}
		});

		pack();
		setVisible(true);

	}

	public void stop() {
		dispose();
	}

	void setCurrent(String fontSt, int size, int style) {
		curFontSt = fontSt;
		curSize = size;
		curStyle = style;

		if (curFontSt.equals("")) {
			curFontSt = defFontSt;
		}
		if (curSize < 1 || curSize > 30) {
			curSize = defSize;
		}
		if (curStyle != Font.PLAIN && curStyle != Font.BOLD
				&& curStyle != Font.ITALIC) {
			curStyle = defStyle;
		}
	}

	void updateLists() {
		{
			int selIndex = 0;
			for (int i = 0; i < fonts.length; i++) {
				if (fonts[i].toUpperCase().equals(curFontSt.toUpperCase())) {
					selIndex = i;
				}
			}
			fontList.setSelectedValue(fonts[selIndex], true);
		}

		{
			int selIndex = 0;
			for (int i = 0; i < sizes.length; i++) {
				int n = Integer.parseInt(sizes[i]);
				if (n == curSize) {
					selIndex = i;
				}
			}
			sizeList.setSelectedValue(sizes[selIndex], true);
		}

		{
			int selIndex = 0;
			for (int i = 0; i < styleId.length; i++) {
				if (styleId[i] == curStyle) {
					selIndex = i;
				}
			}
			styleList.setSelectedValue(styles[selIndex], true);
		}

	}

	void refreshGui() {
		test.setFont(new Font(curFontSt, curStyle, curSize));
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				updateLists();
				repaint();
			}
		});
	}

} // end MyFntSel class