Home | History | Annotate | Download | only in flame
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.tools.flame;
     18 import java.awt.Container;
     19 import java.awt.Cursor;
     20 import java.awt.Font;
     21 import java.awt.GridBagConstraints;
     22 import java.awt.GridBagLayout;
     23 import java.awt.Insets;
     24 import java.awt.event.ActionEvent;
     25 import java.awt.event.ActionListener;
     26 import java.awt.event.MouseAdapter;
     27 import java.awt.event.MouseEvent;
     28 
     29 import javax.swing.BorderFactory;
     30 import javax.swing.JButton;
     31 import javax.swing.JCheckBox;
     32 import javax.swing.JComponent;
     33 import javax.swing.JLabel;
     34 import javax.swing.JPanel;
     35 import javax.swing.JSpinner;
     36 import javax.swing.JToggleButton;
     37 import javax.swing.event.ChangeListener;
     38 import javax.swing.event.TableModelListener;
     39 import javax.swing.table.DefaultTableModel;
     40 
     41 
     42 /** @author Inferno */
     43 public abstract class EditorPanel<T> extends JPanel {
     44 	private String name;
     45 	private String description;
     46 	protected  T value;
     47 	private JPanel titlePanel;
     48 	JToggleButton activeButton;
     49 	JPanel contentPanel;
     50 	JToggleButton advancedButton;
     51 	JButton removeButton;
     52 	JPanel advancedPanel;
     53 	private boolean hasAdvanced;
     54 	JLabel nameLabel, descriptionLabel;
     55 	protected boolean isAlwaysActive, isAlwaysShown = false, isRemovable;
     56 	protected FlameMain editor;
     57 
     58 	public EditorPanel (FlameMain editor, String name, String description, boolean alwaysActive, boolean isRemovable) {
     59 		this.editor = editor;
     60 		this.name = name;
     61 		this.description = description;
     62 		this.isRemovable = isRemovable;
     63 		this.isAlwaysActive = alwaysActive;
     64 		initializeComponents();
     65 		showContent(false);
     66 	}
     67 
     68 	public EditorPanel (FlameMain editor, String name, String description) {
     69 		this(editor, name, description, true, false);
     70 	}
     71 
     72 	protected void activate () {}
     73 
     74 	public void showContent (boolean visible) {
     75 		contentPanel.setVisible(visible);
     76 		advancedPanel.setVisible(visible && advancedButton.isSelected());
     77 		advancedButton.setVisible(visible && hasAdvanced);
     78 		descriptionLabel.setText(visible ? description : "");
     79 	}
     80 
     81 	public void setIsAlwayShown(boolean isAlwaysShown){
     82 		showContent(isAlwaysShown);
     83 		this.isAlwaysShown = isAlwaysShown;
     84 		titlePanel.setCursor(null);
     85 	}
     86 
     87 	public void update (FlameMain editor) {
     88 	}
     89 
     90 	public void setHasAdvanced (boolean hasAdvanced) {
     91 		this.hasAdvanced = hasAdvanced;
     92 		advancedButton.setVisible(hasAdvanced);
     93 	}
     94 
     95 	public JPanel getContentPanel () {
     96 		return contentPanel;
     97 	}
     98 
     99 	public JPanel getAdvancedPanel () {
    100 		return advancedPanel;
    101 	}
    102 
    103 	public String getName () {
    104 		return name;
    105 	}
    106 
    107 	public void setEmbedded () {
    108 		GridBagLayout layout = (GridBagLayout)getLayout();
    109 		GridBagConstraints constraints = layout.getConstraints(contentPanel);
    110 		constraints.insets = new Insets(0, 0, 0, 0);
    111 		layout.setConstraints(contentPanel, constraints);
    112 
    113 		titlePanel.setVisible(false);
    114 	}
    115 
    116 	protected void initializeComponents () {
    117 		setLayout(new GridBagLayout());
    118 		{
    119 			titlePanel = new JPanel(new GridBagLayout());
    120 			add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
    121 				new Insets(3, 0, 3, 0), 0, 0));
    122 			titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    123 			{
    124 				nameLabel = new JLabel(name);
    125 				titlePanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
    126 					new Insets(3, 6, 3, 6), 0, 0));
    127 				nameLabel.setFont(nameLabel.getFont().deriveFont(Font.BOLD));
    128 			}
    129 			{
    130 				descriptionLabel = new JLabel(description);
    131 				titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
    132 					GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
    133 			}
    134 			{
    135 				advancedButton = new JToggleButton("Advanced");
    136 				titlePanel.add(advancedButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    137 					GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    138 				advancedButton.setVisible(false);
    139 			}
    140 			{
    141 				activeButton = new JToggleButton("Active");
    142 				titlePanel.add(activeButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    143 					GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    144 			}
    145 			{
    146 				removeButton = new JButton("X");
    147 				titlePanel.add(removeButton, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    148 					GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    149 			}
    150 		}
    151 		{
    152 			contentPanel = new JPanel(new GridBagLayout());
    153 			add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    154 				new Insets(0, 6, 6, 6), 0, 0));
    155 			contentPanel.setVisible(false);
    156 		}
    157 		{
    158 			advancedPanel = new JPanel(new GridBagLayout());
    159 			add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    160 				new Insets(0, 6, 6, 6), 0, 0));
    161 			advancedPanel.setVisible(false);
    162 		}
    163 
    164 
    165 		titlePanel.addMouseListener(new MouseAdapter() {
    166 			public void mouseClicked (MouseEvent event) {
    167 				if(!isAlwaysShown)
    168 					showContent(!contentPanel.isVisible());
    169 			}
    170 		});
    171 		activeButton.addActionListener(new ActionListener() {
    172 			public void actionPerformed (ActionEvent event) {
    173 				activate();
    174 			}
    175 		});
    176 		advancedButton.addActionListener(new ActionListener() {
    177 			public void actionPerformed (ActionEvent event) {
    178 				advancedPanel.setVisible(advancedButton.isSelected());
    179 			}
    180 		});
    181 
    182 		removeButton.addActionListener(new ActionListener() {
    183 			public void actionPerformed (ActionEvent event) {
    184 				removePanel();
    185 			}
    186 		});
    187 	}
    188 
    189 	protected void removePanel () {
    190 		Container parent = this.getParent();
    191 		parent.remove(this);
    192 		parent.validate();
    193 		parent.repaint();
    194 	}
    195 
    196 	public void setName(String name){
    197 		this.name = name;
    198 		nameLabel.setText(name);
    199 	}
    200 
    201 	public void setDescription(String desc){
    202 		description = desc;
    203 		descriptionLabel.setText(desc);
    204 	}
    205 
    206 
    207 	protected void addContent(int row, int column, JComponent component){
    208 		addContent(row, column, component, true, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    209 	}
    210 
    211 	protected void addContent(int row, int column, JComponent component, boolean addBorder){
    212 		addContent(row, column, component, addBorder, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    213 	}
    214 
    215 	protected void addContent(int row, int column, JComponent component, int anchor, int fill){
    216 		addContent(row, column, component, true, anchor, fill);
    217 	}
    218 
    219 	protected void addContent(int row, int column, JComponent component, boolean addBorders, int anchor, int fill, float wx, float wy){
    220 		addContent(contentPanel, row, column, component, addBorders, anchor, fill, wx, wy);
    221 	}
    222 
    223 	protected void addContent(int row, int column, JComponent component, boolean addBorders, int anchor, int fill){
    224 		addContent(row, column, component, addBorders, anchor, fill, 1, 1);
    225 	}
    226 
    227 	public void setValue (T value) {
    228 		this.value = value;
    229 		activeButton.setVisible(value == null ? false : !isAlwaysActive);
    230 		removeButton.setVisible(isRemovable);
    231 	}
    232 
    233 	public static void addContent( JPanel panel, int row, int column, JComponent component, boolean addBorders, int anchor, int fill, float wx, float wy){
    234 		if(addBorders) component.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, java.awt.Color.black));
    235 		panel.add(component, new GridBagConstraints(column, row, 1, 1, wx, wy, anchor, fill,
    236 			new Insets(0, 0, 0, 0), 0, 0));
    237 	}
    238 
    239 	protected static <K> void setValue(JSpinner spinner, K object){
    240 		ChangeListener[] listeners = spinner.getChangeListeners();
    241 		ChangeListener listener = null;
    242 		if(listeners != null && listeners.length >0){
    243 			listener = listeners[0];
    244 			spinner.removeChangeListener(listener);
    245 		}
    246 		spinner.setValue(object);
    247 		if(listener != null) spinner.addChangeListener(listener);
    248 	}
    249 
    250 	protected static void setValue(JCheckBox checkBox, boolean isSelected){
    251 		ActionListener[] listeners = checkBox.getActionListeners();
    252 		ActionListener listener = null;
    253 		if(listeners != null && listeners.length >0){
    254 			listener = listeners[0];
    255 			checkBox.removeActionListener(listener);
    256 		}
    257 		checkBox.setSelected(isSelected);
    258 		if(listener != null) checkBox.addActionListener(listener);
    259 	}
    260 
    261 	protected static <K> void setValue(Slider slider, float value){
    262 		ChangeListener[] listeners = slider.spinner.getChangeListeners();
    263 		ChangeListener listener = null;
    264 		if(listeners != null && listeners.length >0){
    265 			listener = listeners[0];
    266 			slider.spinner.removeChangeListener(listener);
    267 		}
    268 		slider.setValue(value);
    269 		if(listener != null) slider.spinner.addChangeListener(listener);
    270 	}
    271 
    272 	protected static void setValue (DefaultTableModel tableModel, Object value, int row, int column) {
    273 		TableModelListener[] listeners = tableModel.getTableModelListeners();
    274 		TableModelListener listener = null;
    275 		if(listeners != null && listeners.length >0){
    276 			listener = listeners[0];
    277 			tableModel.removeTableModelListener(listener);
    278 		}
    279 		tableModel.setValueAt(value, row, column);
    280 		if(listener != null) tableModel.addTableModelListener(listener);
    281 	}
    282 
    283 
    284 }
    285