001: package snow.utils.gui;
002:
003: import snow.utils.SysUtils;
004: import tide.annotations.ReturnAPassedArg;
005: import snow.texteditor.SimpleDocument;
006: import javax.swing.text.JTextComponent;
007: import java.awt.Color;
008: import javax.swing.border.LineBorder;
009: import java.util.*;
010: import java.awt.event.*;
011: import java.awt.Dimension;
012: import java.awt.Toolkit;
013: import java.awt.FlowLayout;
014: import java.awt.Component;
015: import java.awt.Insets;
016: import javax.swing.border.EmptyBorder;
017: import java.awt.BorderLayout;
018: import javax.swing.*;
019:
020: /** Graphical utilities.
021: */
022: public final class GUIUtils {
023: private GUIUtils() {
024: }
025:
026: public static void displayInFrame(String title, JComponent c,
027: boolean exitOnClose) {
028: JFrame f = new JFrame(title);
029: if (exitOnClose)
030: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
031: f.add(c, BorderLayout.CENTER);
032: f.add(new CloseControlPanel(f, false, false, "Close"),
033: BorderLayout.SOUTH);
034: f.pack();
035: Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
036: if (f.getWidth() > screen.getWidth()) {
037: f.setSize(new Dimension((int) screen.getWidth() * 8 / 10, f
038: .getHeight() * 9 / 10));
039: }
040:
041: if (f.getHeight() > screen.getHeight()) {
042: f.setSize(new Dimension(f.getWidth(), (int) screen
043: .getHeight() * 9 / 10));
044: }
045:
046: f.setLocationRelativeTo(null);
047: f.setVisible(true);
048: }
049:
050: // non modal.
051: public static JDialog displayInDialog(Component parent,
052: String title, JComponent c) {
053: JDialog d = null;
054: if (parent instanceof JFrame) {
055: d = new JDialog((JFrame) parent, title, false);
056: } else if (parent instanceof JDialog) {
057: d = new JDialog((JDialog) parent, title, false);
058: }
059: d.add(c, BorderLayout.CENTER);
060: d.add(new CloseControlPanel(d, false, false, "Close"),
061: BorderLayout.SOUTH);
062: d.pack();
063: Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
064: if (d.getWidth() > screen.getWidth()) {
065: d.setSize(new Dimension((int) screen.getWidth() * 8 / 10, d
066: .getHeight() * 9 / 10));
067: }
068:
069: if (d.getHeight() > screen.getHeight()) {
070: d.setSize(new Dimension(d.getWidth(), (int) screen
071: .getHeight() * 9 / 10));
072: }
073:
074: d.setLocationRelativeTo(parent);
075: d.setVisible(true);
076: return d;
077: }
078:
079: /**
080: */
081: @ReturnAPassedArg("b")
082: public static JButton makeNiceButton(final JButton b) {
083: b.setMargin(new Insets(0, 2, 0, 2));
084: b.setFocusPainted(false);
085: b.setAlignmentY(0.75f); // for the baseline when added in a text component.
086: return b;
087: }
088:
089: public static JButton niceMediumButton(String txt) {
090: JButton bu = new JButton(txt);
091: makeNiceButton(bu);
092: return bu;
093: }
094:
095: public static JButton createClickableURL(final String ref) {
096: final JButton bt = new JButton(ref);
097: //AttributeSet pe = this.getStyle("default");
098: //bt.setFont(getFontFrom(pe));
099: makeNiceButton(bt);
100: bt.addActionListener(new ActionListener() {
101: public void actionPerformed(ActionEvent ae) {
102: try {
103: SysUtils.openBrowser(ref);
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107: }
108: });
109: return bt;
110: }
111:
112: /** With a light yellowed background derived from UIManager.getColor("Panel.background").
113: */
114: public static JTextComponent createReadOnlyDescriptionArea(
115: String d, JComponent... replacementsFor$$) {
116: final JTextComponent ta;
117:
118: if (replacementsFor$$.length > 0) {
119: SimpleDocument doc = new SimpleDocument();
120: ta = new JTextPane(doc);
121: doc.append(d);
122: int i = 1;
123: for (JComponent ci : replacementsFor$$) {
124: int pos = doc.search("$$" + i, 0, false);
125: if (pos < 0) {
126: System.out
127: .println("$$" + i + ": not found in " + d);
128: doc.append(ci);
129: } else {
130: doc.append(ci, pos, 3);
131: }
132: i++;
133: }
134:
135: } else {
136:
137: if (d.startsWith("<html>")) {
138: ta = new JTextPane();
139: ((JTextPane) ta).setContentType("text/html");
140: ta.setText(d);
141:
142: } else {
143: ta = new JTextArea(d);
144: }
145: }
146: ta.setBorder(new EmptyBorder(3, 5, 8, 2));
147: //ta.setOpaque(false);
148: ta.setEditable(false);
149: ta.setBackground(giveYellowTouch(UIManager
150: .getColor("Panel.background")));
151: return ta;
152: }
153:
154: public static Color giveYellowTouch(Color c) {
155: // only approximative !
156: int oldb = c.getBlue();
157: return new Color(c.getRed(), c.getGreen(),
158: (oldb > 128 ? oldb * 10 / 11 : oldb * 11 / 10));
159: }
160:
161: public static JTextField createHelpLabel(String d) {
162: JTextField ta = new JTextField(d);
163: ta.setBorder(new EmptyBorder(2, 5, 2, 2));
164: ta.setOpaque(false);
165: ta.setEditable(false);
166: return ta;
167: }
168:
169: public static JPanel wrapLeft(Component c, int marginx) {
170: JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT,
171: marginx, 0));
172: pan.add(c);
173: return pan;
174: }
175:
176: /** Adds one of the item on right click. Only consider the first 50 ones...
177: */
178: @ReturnAPassedArg("tf")
179: public static JTextField offerRightClickAddCompletion(
180: final JTextField tf, final List<String> cps) {
181: if (cps == null || cps.size() == 0)
182: return tf;
183:
184: tf.addMouseListener(new MouseAdapter() {
185: @Override
186: public void mousePressed(MouseEvent me) {
187: if (me.isPopupTrigger()) {
188: showPopup(me);
189: }
190: }
191:
192: @Override
193: public void mouseReleased(MouseEvent me) {
194: if (me.isPopupTrigger()) {
195: showPopup(me);
196: }
197: }
198:
199: public void showPopup(MouseEvent me) {
200: JPopupMenu pop = new JPopupMenu();
201: int n = 0;
202: for (final String si : cps) {
203: n++;
204: if (n > 50) {
205: //... just ignore
206: break;
207: }
208: JMenuItem mi = new JMenuItem(si, Icons.sharedPlus);
209: pop.add(mi);
210: mi.addActionListener(new ActionListener() {
211: public void actionPerformed(ActionEvent ae) {
212: if (tf.getText().trim().length() > 0) {
213: tf.setText(tf.getText() + " " + si);
214: } else {
215: tf.setText(si);
216: }
217: }
218: });
219: }
220: pop.show(tf, 0, tf.getHeight());
221: }
222: });
223: return tf;
224: }
225:
226: public static JWindow showQuickSplash(final long millis,
227: final String txt, Component compForCenter) {
228: final JWindow win = new JWindow();
229:
230: JPanel cp = new JPanel(new BorderLayout());
231: win.add(cp, BorderLayout.CENTER);
232: cp.setBorder(new LineBorder(Color.black));
233:
234: JTextArea ta = new JTextArea(txt);
235: ta.setFont(UIManager.getFont("Label.font"));
236: ta.setBorder(new EmptyBorder(10, 10, 10, 10));
237: cp.setBackground(ta.getBackground());
238: cp.add(ta, BorderLayout.CENTER);
239:
240: JPanel np = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 1));
241: JButton close = new JButton(Icons.sharedCross);
242: np.add(close);
243: close.setMargin(new Insets(0, 0, 0, 0));
244: cp.add(np, BorderLayout.NORTH);
245: close.addActionListener(new ActionListener() {
246: public void actionPerformed(ActionEvent ae) {
247: win.setVisible(false);
248: }
249: });
250:
251: win.pack();
252: win.setLocationRelativeTo(compForCenter);
253: win.setVisible(true);
254:
255: Thread t = new Thread() {
256: public void run() {
257: try {
258: Thread.sleep(millis);
259: } catch (InterruptedException ex) {
260: }
261: win.setVisible(false);
262: }
263: };
264: t.start();
265:
266: return win;
267: }
268:
269: }
|