/* * * GeneralLayerUI.java version 0.1 * * Copyright (C) 2008 Piet Blok. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Created on 3 aug 2008 */ package org.pbjar.jxlayer.test; import java.awt.Component; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.MouseWheelEvent; import java.util.ArrayList; import java.util.List; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.SwingUtilities; import org.jdesktop.jxlayer.JXLayer; import org.jdesktop.jxlayer.plaf.AbstractLayerUI; public abstract class GeneralLayerUI extends AbstractLayerUI { /** * This inner class encapsulates state information specific to one JXLayer. * One GeneralLayerUI may be set to multiple JXLayers. This StateObject must * hold information that is specific to the combination of a JXLayer and a * LayerUI. */ protected abstract class StateObject { /** * Clear this StateObject and stop pending operations. *

* This is an empty implementation; *

* * @param layer * the specific JXLayer for this StateObject */ public void uninstall(JXLayer layer) { } } private final String stateKey = this.getClass().getName() + ".stateKey"; /** * Get actions that are applicable to this LayerUI. This implementation * returns an enable/disable action for this LayerUI * * @return a list of applicable actions */ public List getActions() { ArrayList actionList = new ArrayList(); actionList.add(new FlipFlopAction("Enable " + this.getClass().getSimpleName()) { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent event) { GeneralLayerUI.this.setEnabled(!this.getState()); } @Override public boolean getState() { return GeneralLayerUI.this.isEnabled(); } }); return actionList; } /** * Get actions that are applicable to this LayerUI and a specific JXLayer. * This implementation returns an empty list * * @param layer * the JXLayer * * @return a list of applicable actions */ public List getActions(JXLayer layer) { ArrayList actionList = new ArrayList(); return actionList; } public String getName() { return this.getClass().getSimpleName(); } /** * Invokes super.installUI. Then invokes createStateObject. User * installation actions may be coded in createStateObject. */ @SuppressWarnings("unchecked") @Override public final void installUI(JComponent c) { super.installUI(c); JXLayer layer = (JXLayer) c; StateObject stateObject = createStateObject(layer); if (stateObject != null) { layer.putClientProperty(stateKey, stateObject); } } @Override protected void processMouseWheelEvent(MouseWheelEvent e, JXLayer jxlayer) { super.processMouseWheelEvent(e, jxlayer); Component parent = jxlayer.getParent(); if (parent != null) { Point point = SwingUtilities.convertPoint(e.getComponent(), e .getPoint(), parent); MouseWheelEvent newEvent = new MouseWheelEvent(parent, // e.getID(), // e.getWhen(), // e.getModifiers(), // point.x, // point.y, // e.getClickCount(), // e.isPopupTrigger(), // e.getScrollType(), // e.getScrollAmount(), // e.getWheelRotation() // ); parent.dispatchEvent(newEvent); } } /** * Invokes super.uninstallUI. Then invokes StateObject.uninstall. User * uninstallation actions may be coded on StateObject.uninstall. */ @SuppressWarnings("unchecked") public final void uninstallUI(JComponent c) { super.uninstallUI(c); JXLayer layer = (JXLayer) c; StateObject stateObject = getStateObject(layer); if (stateObject != null) { stateObject.uninstall(layer); layer.putClientProperty(stateKey, null); } } /** * Create a StateObject specific for this LayerUI and the JXLayer argument. * * @param layer * the JXLayer * @return a StateObject or null, if no state is maintained. */ protected abstract StateObject createStateObject(JXLayer layer); /** * Get the created StateObject specific to the JXLayer argument. * * @param layer * the JXLayer * @return the StateObject or null, if createStateObject returned null */ protected StateObject getStateObject(JXLayer layer) { return (StateObject) layer.getClientProperty(stateKey); } @Override protected boolean isAWTEventListenerEnabled() { return true; } }