2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - Clement DAVID
4 * Copyright (C) 2011-2017 - Scilab Enterprises - Clement DAVID
5 * Copyright (C) 2015 - Marcos CARDINOT
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
18 package org.scilab.modules.xcos.palette.view;
20 import java.awt.AlphaComposite;
21 import java.awt.Color;
22 import java.awt.Dimension;
23 import java.awt.FlowLayout;
24 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.Rectangle;
27 import java.awt.event.FocusEvent;
28 import java.awt.event.FocusListener;
29 import java.awt.geom.Rectangle2D;
31 import javax.swing.JPanel;
32 import javax.swing.Scrollable;
33 import javax.swing.SwingConstants;
35 import org.scilab.modules.xcos.utils.XcosConstants;
36 import org.scilab.modules.xcos.utils.XcosConstants.PaletteBlockSize;
39 * Implement a view of a block category.
41 * As the JScrollPane doesn't allow a to specify it's associated layout, we have
42 * to use a mainArea to perform what we want there. Never use the
43 * {@link javax.swing.JScrollPane#add(java.awt.Component)} on this class but use
44 * {@link JPanel#add(java.awt.Component)} instead.
46 @SuppressWarnings(value = { "serial" })
47 public class PaletteView extends JPanel implements Scrollable {
49 private static Rectangle2D.Double selectionRect;
50 private boolean isLoaded;
55 public PaletteView() {
56 setName("PaletteView");
60 /** Setup component */
61 private void initComponents() {
62 setBackground(Color.WHITE);
64 setLayout(new ModifiedFlowLayout(FlowLayout.LEADING,
65 XcosConstants.PALETTE_HMARGIN,
66 XcosConstants.PALETTE_VMARGIN));
68 // if this panel gains focus, try to select the first block!
70 addFocusListener(new FocusListener() {
72 public void focusLost(FocusEvent e) {
75 public void focusGained(FocusEvent e) {
77 if (getComponentCount() > 0) {
78 ((PaletteBlockView) getComponent(0)).getController().setSelected(true);
80 } catch (ClassCastException err) {
81 } catch (NullPointerException err) {
88 * Sets the selection rectangle
89 * @param rect Rectangle2D
91 public void setSelectionRectangle(Rectangle2D.Double rect) {
97 * @return true if the view is already loaded
99 public boolean isLoaded() {
104 * This class update the status of the view. If you want to save memory,
105 * unload the view when hidden.
110 public void setLoaded(boolean b) {
118 * @return The prefered Scrollable dimension
119 * @see javax.swing.Scrollable#getPreferredScrollableViewportSize()
122 public Dimension getPreferredScrollableViewportSize() {
123 return getPreferredSize();
128 * The view area visible within the viewport
130 * Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
132 * Less than zero to scroll up/left, greater than zero for
134 * @return The "block" increment for scrolling in the specified direction.
135 * This value should always be positive.
136 * @see javax.swing.Scrollable#getScrollableBlockIncrement(java.awt.Rectangle,
140 public int getScrollableBlockIncrement(Rectangle visibleRect,
141 int orientation, int direction) {
142 PaletteBlockSize palBlockSize = PaletteManagerPanel.getCurrentSize();
143 if (orientation == SwingConstants.VERTICAL) {
144 return palBlockSize.getBlockDimension().height
145 + XcosConstants.PALETTE_VMARGIN;
147 return palBlockSize.getBlockDimension().width
148 + XcosConstants.PALETTE_HMARGIN;
153 * @return always false
154 * @see javax.swing.Scrollable#getScrollableTracksViewportHeight()
157 public boolean getScrollableTracksViewportHeight() {
162 * @return always true
163 * @see javax.swing.Scrollable#getScrollableTracksViewportWidth()
166 public boolean getScrollableTracksViewportWidth() {
172 * The view area visible within the viewport
174 * Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
176 * Less than zero to scroll up/left, greater than zero for
178 * @return PALETTE_BLOCK_HEIGHT or PALETTE_BLOCK_WIDTH depending on
180 * @see javax.swing.Scrollable#getScrollableUnitIncrement(java.awt.Rectangle,
184 public int getScrollableUnitIncrement(Rectangle visibleRect,
185 int orientation, int direction) {
186 PaletteBlockSize palBlockSize = PaletteManagerPanel.getCurrentSize();
187 if (orientation == SwingConstants.VERTICAL) {
188 return palBlockSize.getBlockDimension().height;
190 return palBlockSize.getBlockDimension().width;
195 * Paints the selection rectangle.
199 protected void paintComponent(Graphics g) {
200 super.paintComponent(g);
201 if (selectionRect == null) {
205 Graphics2D g2d = (Graphics2D) g;
206 final float alpha = 0.1f;
207 AlphaComposite ta = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
208 g2d.setComposite(ta);
209 g2d.setColor(Color.BLUE);
210 g2d.fill(selectionRect);
211 g2d.setComposite(AlphaComposite.SrcOver);
212 g2d.setColor(Color.BLACK);
213 g2d.draw(selectionRect);