summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/QFileChooser.java
blob: ddac98f0c18c323d289c243f78e6d62b72fab7b4 (plain) (blame)
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
package org.openslx.dozmod.gui.helper;

import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

public class QFileChooser extends JFileChooser {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 3856532034624815076L;

	private static final Logger LOGGER = Logger.getLogger(QFileChooser.class);

	public QFileChooser(String path, boolean dirMode) {
		super(path);

		// Ugly hack to get a prettier file chooser with GTK
		LookAndFeel old = UIManager.getLookAndFeel();
		if (old == null || !old.getName().toLowerCase().contains("gtk")) {
			old = null;
		} else {
			try {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception e) {
				LOGGER.error("Unable to set cross-platform 'Look and Feel'", e);
			}
		}
		if (old != null) {
			updateUI();
			try {
				UIManager.setLookAndFeel(old);
			} catch (Exception e) {
				LOGGER.error("Unable to reset 'Look and Feel'", e);
			}
			refreshUI(this, false);
		}

		setFileSelectionMode(dirMode ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY);
	}

	private static void refreshUI(JComponent c, boolean includeParent) {
		if (includeParent)
			c.updateUI();

		for (int i = 0; i < c.getComponentCount(); i++) {
			Component child = c.getComponent(i);
			if (child instanceof JComponent) {
				refreshUI((JComponent) child, true);
			}
		}
	}
}