PageRenderTime 74ms CodeModel.GetById 65ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/ui/PanelUtils.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 172 lines | 117 code | 27 blank | 28 comment | 19 complexity | 8a9c85afbd45c7636af223f89f72a5dd MD5 | raw file
  1
  2/*
  3 *
  4 *
  5 */
  6package mpv5.utils.ui;
  7
  8//~--- non-JDK imports --------------------------------------------------------
  9import mpv5.ui.beans.LabeledTextField;
 10
 11//~--- JDK imports ------------------------------------------------------------
 12
 13import java.awt.AlphaComposite;
 14import java.awt.Color;
 15import java.awt.Component;
 16import java.awt.Font;
 17import java.awt.Graphics;
 18import java.awt.Graphics2D;
 19import java.awt.event.ActionEvent;
 20import java.awt.event.ActionListener;
 21
 22import java.beans.PropertyChangeEvent;
 23import java.beans.PropertyChangeListener;
 24
 25import java.util.HashMap;
 26import javax.swing.*;
 27import mpv5.ui.dialogs.subcomponents.ControlPanel_Company;
 28import mpv5.utils.text.TypeConversion;
 29
 30/**
 31 *
 32 *
 33 */
 34public class PanelUtils {
 35
 36    /**
 37     * Cuts the text of a textfield to the given length
 38     * @param jTextField
 39     * @param length
 40     */
 41    public static void cut(JTextField jTextField, int length) {
 42        try {
 43            jTextField.setText(jTextField.getText().substring(0, length));
 44        } catch (Exception exception) {
 45        }
 46    }
 47
 48    /**
 49     * Clears the text of the given components'
 50     * JTextfieds, LabeledTextFields, JEditorpanes and JTextareas
 51     * @param panel
 52     */
 53    public static void clearText(JPanel panel) {
 54        JTextField jt = null;
 55        Object p;
 56        JEditorPane ja = null;
 57        JTextArea je = null;
 58        LabeledTextField le = null;
 59
 60        for (int i = 0; i < panel.getComponents().length; i++) {
 61            try {
 62                p = (java.lang.Object) panel.getComponents()[i];
 63
 64                if (p.getClass().isInstance(new JTextField())) {
 65                    jt = (JTextField) panel.getComponents()[i];
 66                    jt.setText("");
 67                }
 68
 69                if (p.getClass().isInstance(new JEditorPane())) {
 70                    ja = (JEditorPane) panel.getComponents()[i];
 71                    ja.setText(null);
 72                }
 73
 74                if (p.getClass().isInstance(new JTextArea())) {
 75                    je = (JTextArea) panel.getComponents()[i];
 76                    je.setText("");
 77                }
 78
 79                if (p.getClass().isInstance(new LabeledTextField())) {
 80                    le = (LabeledTextField) panel.getComponents()[i];
 81                    le.set_Text("");
 82                }
 83            } catch (Exception exception) {
 84            }
 85        }
 86    }
 87
 88    /**
 89     * Enables/disables the subcomponents of a panel entirely (not the panel itself!)
 90     * @param component
 91     * @param state True means enabled, false disabled
 92     */
 93    public static void enableSubComponents(JComponent component, boolean state) {
 94        for (int i = 0; i < component.getComponents().length; i++) {
 95            component.getComponents()[i].setEnabled(state);
 96        }
 97    }
 98
 99    public static void setTitle(JPanel aThis, String cname_) {
100        if (aThis.getParent() instanceof JViewport || aThis.getParent() instanceof JTabbedPane) {
101            JTabbedPane jTabbedPane = null;
102            String title1 = cname_;
103            //this->viewport->scrollpane->tabbedpane
104            if (aThis.getParent().getParent().getParent() instanceof JTabbedPane) {
105                jTabbedPane = (JTabbedPane) aThis.getParent().getParent().getParent();
106            } else {
107                try {
108                    jTabbedPane = (JTabbedPane) aThis.getParent();
109                } catch (Exception e) {
110                    //Free floating window
111                    ((JFrame) aThis.getRootPane().getParent()).setTitle(title1);
112                }
113            }
114            if (jTabbedPane != null) {
115                jTabbedPane.setTitleAt(jTabbedPane.getSelectedIndex(), title1);
116            }
117        }
118    }
119
120    public static HashMap<String, String> getSubComponentValues(JComponent panel) {
121        HashMap<String, String> m = new HashMap<String, String>();
122        JTextField jt = null;
123        Object p;
124        JEditorPane ja = null;
125        JTextArea je = null;
126        JCheckBox jc = null;
127        JRadioButton jr = null;
128        LabeledTextField le = null;
129
130        for (int i = 0; i < panel.getComponents().length; i++) {
131            try {
132                p = (java.lang.Object) panel.getComponents()[i];
133
134                if (p.getClass().isInstance(new JTextField())) {
135                    jt = (JTextField) panel.getComponents()[i];
136                    m.put(jt.getName(), jt.getText());
137                }
138
139                if (p.getClass().isInstance(new JEditorPane())) {
140                    ja = (JEditorPane) panel.getComponents()[i];
141                    m.put(ja.getName(), ja.getText());
142                }
143
144                if (p.getClass().isInstance(new JTextArea())) {
145                    je = (JTextArea) panel.getComponents()[i];
146                    m.put(je.getName(), je.getText());
147                }
148
149                if (p.getClass().isInstance(new LabeledTextField())) {
150                    le = (LabeledTextField) panel.getComponents()[i];
151                    m.put(le.getName(), le.getText());
152                }
153                
154                if (p.getClass().isInstance(new JRadioButton())) {
155                    jr = (JRadioButton) panel.getComponents()[i];
156                    m.put(jr.getName(), TypeConversion.booleanToString(jr.isSelected()));
157                }
158                
159                if (p.getClass().isInstance(new JCheckBox())) {
160                    jc = (JCheckBox) panel.getComponents()[i];
161                    m.put(jc.getName(), TypeConversion.booleanToString(jc.isSelected()));
162                }
163            } catch (Exception exception) {
164            }
165        }
166        
167        return m;
168    }
169}
170
171
172//~ Formatted by Jindent --- http://www.jindent.com