001/** 002 * 003 */ 004package icy.gui.inspector; 005 006import icy.action.CanvasActions; 007import icy.canvas.IcyCanvas; 008import icy.canvas.Layer; 009import icy.gui.component.button.IcyButton; 010import icy.gui.viewer.Viewer; 011import icy.main.Icy; 012import icy.system.thread.ThreadUtil; 013 014import java.awt.BorderLayout; 015import java.awt.Dimension; 016import java.awt.GridBagConstraints; 017import java.awt.GridBagLayout; 018import java.awt.Insets; 019import java.util.List; 020 021import javax.swing.JLabel; 022import javax.swing.JPanel; 023import javax.swing.JScrollPane; 024import javax.swing.JSlider; 025import javax.swing.ScrollPaneConstants; 026import javax.swing.event.ChangeEvent; 027import javax.swing.event.ChangeListener; 028 029/** 030 * @author Stephane 031 */ 032public class LayerControlPanel extends JPanel implements ChangeListener 033{ 034 /** 035 * 036 */ 037 private static final long serialVersionUID = 6501341338561271486L; 038 039 // GUI 040 JSlider opacitySlider; 041 IcyButton deleteButton; 042 043 // internal 044 final LayersPanel layerPanel; 045 private JPanel optionsPanel; 046 047 public LayerControlPanel(LayersPanel layerPanel) 048 { 049 super(); 050 051 this.layerPanel = layerPanel; 052 053 initialize(); 054 055 opacitySlider.addChangeListener(this); 056 } 057 058 private void initialize() 059 { 060 setBorder(null); 061 setLayout(new BorderLayout(0, 0)); 062 063 JScrollPane scrollPane = new JScrollPane(); 064 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 065 add(scrollPane, BorderLayout.CENTER); 066 067 final JPanel actionPanel = new JPanel(); 068 scrollPane.setViewportView(actionPanel); 069 scrollPane.setMaximumSize(new Dimension(32767, 100)); 070 GridBagLayout gbl_actionPanel = new GridBagLayout(); 071 gbl_actionPanel.columnWidths = new int[] {0, 0, 0, 0}; 072 gbl_actionPanel.rowHeights = new int[] {0, 0, 0}; 073 gbl_actionPanel.columnWeights = new double[] {1.0, 1.0, 0.0, Double.MIN_VALUE}; 074 gbl_actionPanel.rowWeights = new double[] {1.0, 0.0, Double.MIN_VALUE}; 075 actionPanel.setLayout(gbl_actionPanel); 076 077 optionsPanel = new JPanel(); 078 GridBagConstraints gbc_optionsPanel = new GridBagConstraints(); 079 gbc_optionsPanel.gridwidth = 3; 080 gbc_optionsPanel.insets = new Insets(0, 0, 5, 5); 081 gbc_optionsPanel.fill = GridBagConstraints.BOTH; 082 gbc_optionsPanel.gridx = 0; 083 gbc_optionsPanel.gridy = 0; 084 actionPanel.add(optionsPanel, gbc_optionsPanel); 085 optionsPanel.setLayout(new BorderLayout(0, 0)); 086 087 final JLabel lblOpacity = new JLabel(" Opacity "); 088 GridBagConstraints gbc_lblOpacity = new GridBagConstraints(); 089 gbc_lblOpacity.anchor = GridBagConstraints.WEST; 090 gbc_lblOpacity.insets = new Insets(0, 0, 0, 5); 091 gbc_lblOpacity.gridx = 0; 092 gbc_lblOpacity.gridy = 1; 093 actionPanel.add(lblOpacity, gbc_lblOpacity); 094 095 opacitySlider = new JSlider(); 096 opacitySlider.setPreferredSize(new Dimension(120, 23)); 097 opacitySlider.setFocusable(false); 098 opacitySlider.setMinimumSize(new Dimension(120, 23)); 099 GridBagConstraints gbc_opacitySlider = new GridBagConstraints(); 100 gbc_opacitySlider.fill = GridBagConstraints.HORIZONTAL; 101 gbc_opacitySlider.insets = new Insets(0, 0, 0, 5); 102 gbc_opacitySlider.gridx = 1; 103 gbc_opacitySlider.gridy = 1; 104 actionPanel.add(opacitySlider, gbc_opacitySlider); 105 106 deleteButton = new IcyButton(CanvasActions.deleteLayersAction); 107 deleteButton.setFlat(true); 108 GridBagConstraints gbc_deleteButton = new GridBagConstraints(); 109 gbc_deleteButton.anchor = GridBagConstraints.EAST; 110 gbc_deleteButton.gridx = 2; 111 gbc_deleteButton.gridy = 1; 112 actionPanel.add(deleteButton, gbc_deleteButton); 113 114 validate(); 115 } 116 117 public void refresh() 118 { 119 final List<Layer> selectedLayers = layerPanel.getSelectedLayers(); 120 final boolean hasSelected = (selectedLayers.size() > 0); 121 final boolean singleSelected = (selectedLayers.size() == 1); 122 final Layer firstSelected = hasSelected ? selectedLayers.get(0) : null; 123 124 // boolean canEdit = false; 125 boolean canRemove = false; 126 127 for (Layer layer : selectedLayers) 128 { 129 // canEdit |= !layer.isReadOnly(); 130 canRemove |= layer.getCanBeRemoved(); 131 } 132 133 final boolean canRemovef = canRemove; 134 135 ThreadUtil.invokeNow(new Runnable() 136 { 137 @Override 138 public void run() 139 { 140 if (hasSelected) 141 { 142 opacitySlider.setValue((int) (firstSelected.getOpacity() * 100f)); 143 opacitySlider.setEnabled(true); 144 deleteButton.setEnabled(canRemovef); 145 } 146 else 147 { 148 opacitySlider.setEnabled(false); 149 deleteButton.setEnabled(false); 150 } 151 152 optionsPanel.setVisible(false); 153 optionsPanel.removeAll(); 154 155 if (singleSelected) 156 { 157 final JPanel panel = firstSelected.getOverlay().getOptionsPanel(); 158 if (panel != null) 159 { 160 optionsPanel.add(panel); 161 optionsPanel.setVisible(true); 162 } 163 } 164 165 if (getParent() != null) getParent().validate(); 166 else revalidate(); 167 } 168 }); 169 } 170 171 @Override 172 public void stateChanged(ChangeEvent e) 173 { 174 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 175 176 if (viewer != null) 177 { 178 final IcyCanvas canvas = viewer.getCanvas(); 179 180 if (canvas != null) 181 { 182 final List<Layer> selectedLayers = layerPanel.getSelectedLayers(); 183 final int value = opacitySlider.getValue(); 184 185 if (selectedLayers.size() > 0) 186 { 187 canvas.beginUpdate(); 188 try 189 { 190 // set layer transparency 191 for (Layer layer : selectedLayers) 192 layer.setOpacity(value / 100f); 193 } 194 finally 195 { 196 canvas.endUpdate(); 197 } 198 } 199 } 200 } 201 202 } 203}