You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/07/20 01:45:37 UTC
svn commit: r965680 - in /pivot/trunk: tests/src/org/apache/pivot/tests/
wtk/src/org/apache/pivot/wtk/ wtk/src/org/apache/pivot/wtk/skin/
Author: gbrown
Date: Mon Jul 19 23:45:36 2010
New Revision: 965680
URL: http://svn.apache.org/viewvc?rev=965680&view=rev
Log:
Add experimental support for SwingAdapter.
Added:
pivot/trunk/tests/src/org/apache/pivot/tests/SwingAdapterTest.java
pivot/trunk/tests/src/org/apache/pivot/tests/swing_adapter_test.bxml
pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapter.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapterListener.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/SwingAdapterSkin.java
Modified:
pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java
Added: pivot/trunk/tests/src/org/apache/pivot/tests/SwingAdapterTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/SwingAdapterTest.java?rev=965680&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/SwingAdapterTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/SwingAdapterTest.java Mon Jul 19 23:45:36 2010
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+package org.apache.pivot.tests;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.MouseWheelListener;
+import java.net.URL;
+
+import javax.swing.JButton;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+import org.apache.pivot.beans.Bindable;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.util.Resources;
+import org.apache.pivot.wtk.Window;
+
+public class SwingAdapterTest extends Window implements Bindable {
+ static {
+ try {
+ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
+ } catch (UnsupportedLookAndFeelException exception) {
+ System.err.println(exception);
+ } catch (ClassNotFoundException exception) {
+ System.err.println(exception);
+ } catch (InstantiationException exception) {
+ System.err.println(exception);
+ } catch (IllegalAccessException exception) {
+ System.err.println(exception);
+ }
+ }
+
+ @Override
+ public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
+ JButton swingButton = (JButton)namespace.get("swingButton");
+
+ swingButton.addMouseListener(new MouseListener() {
+ @Override
+ public void mouseExited(MouseEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void mousePressed(MouseEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void mouseReleased(MouseEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void mouseClicked(MouseEvent event) {
+ System.out.println(event);
+ }
+ });
+
+ swingButton.addMouseMotionListener(new MouseMotionListener() {
+ @Override
+ public void mouseMoved(MouseEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void mouseDragged(MouseEvent event) {
+ System.out.println(event);
+ }
+ });
+
+ swingButton.addMouseWheelListener(new MouseWheelListener() {
+ @Override
+ public void mouseWheelMoved(MouseWheelEvent event) {
+ System.out.println(event);
+ }
+ });
+
+ swingButton.addKeyListener(new KeyListener() {
+ @Override
+ public void keyTyped(KeyEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void keyReleased(KeyEvent event) {
+ System.out.println(event);
+ }
+
+ @Override
+ public void keyPressed(KeyEvent event) {
+ System.out.println(event);
+ }
+ });
+ }
+}
Added: pivot/trunk/tests/src/org/apache/pivot/tests/swing_adapter_test.bxml
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/swing_adapter_test.bxml?rev=965680&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/swing_adapter_test.bxml (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/swing_adapter_test.bxml Mon Jul 19 23:45:36 2010
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to you 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.
+-->
+
+<tests:SwingAdapterTest title="Swing Adapter Test" maximized="true"
+ xmlns:bxml="http://pivot.apache.org/bxml"
+ xmlns:tests="org.apache.pivot.tests"
+ xmlns:swing="javax.swing"
+ xmlns="org.apache.pivot.wtk">
+ <Border styles="{padding:8}">
+ <BoxPane orientation="vertical">
+ <PushButton buttonData="Pivot Button"/>
+ <SwingAdapter>
+ <swingComponent>
+ <swing:JButton bxml:id="swingButton" text="Swing Button"/>
+ </swingComponent>
+ </SwingAdapter>
+
+ <TextInput text="Pivot Text Input"/>
+ <SwingAdapter>
+ <swingComponent>
+ <swing:JTextField bxml:id="swingTextField" text="Swing Text Field"/>
+ </swingComponent>
+ </SwingAdapter>
+ </BoxPane>
+ </Border>
+</tests:SwingAdapterTest>
+
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java?rev=965680&r1=965679&r2=965680&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java Mon Jul 19 23:45:36 2010
@@ -73,6 +73,7 @@ public abstract class ApplicationContext
private static final long serialVersionUID = -815713849595314026L;
private Display display = new Display(this);
+ private AWTEvent currentAWTEvent = null;
private Component focusedComponent = null;
@@ -299,6 +300,10 @@ public abstract class ApplicationContext
return display;
}
+ public AWTEvent getCurrentAWTEvent() {
+ return currentAWTEvent;
+ }
+
public double getScale() {
return scale;
}
@@ -657,7 +662,12 @@ public abstract class ApplicationContext
@Override
protected void processEvent(AWTEvent event) {
+ currentAWTEvent = event;
+
super.processEvent(event);
+
+ currentAWTEvent = null;
+
display.validate();
}
Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapter.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapter.java?rev=965680&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapter.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapter.java Mon Jul 19 23:45:36 2010
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+package org.apache.pivot.wtk;
+
+import java.awt.DefaultKeyboardFocusManager;
+import java.awt.KeyboardFocusManager;
+
+import javax.swing.JComponent;
+import javax.swing.RepaintManager;
+
+import org.apache.pivot.util.ListenerList;
+
+/**
+ * Allows a Swing component to be used within a Pivot application.
+ */
+public class SwingAdapter extends Component {
+ private static class SwingAdapterListenerList extends ListenerList<SwingAdapterListener>
+ implements SwingAdapterListener {
+ public void swingComponentChanged(SwingAdapter swingAdapter, JComponent previousSwingComponent) {
+ for (SwingAdapterListener listener : this) {
+ listener.swingComponentChanged(swingAdapter, previousSwingComponent);
+ }
+ }
+ }
+
+ static {
+ RepaintManager.setCurrentManager(new SwingAdapterRepaintManager());
+ KeyboardFocusManager.setCurrentKeyboardFocusManager(new SwingAdapterKeyboardFocusManager());
+ }
+
+ private JComponent swingComponent = null;
+
+ private SwingAdapterListenerList swingAdapterListeners = new SwingAdapterListenerList();
+
+ public SwingAdapter() {
+ installThemeSkin(SwingAdapter.class);
+ }
+
+ public JComponent getSwingComponent() {
+ return swingComponent;
+ }
+
+ public void setSwingComponent(JComponent swingComponent) {
+ JComponent previousSwingComponent = this.swingComponent;
+
+ if (previousSwingComponent != swingComponent) {
+ if (previousSwingComponent != null) {
+ previousSwingComponent.putClientProperty(SwingAdapter.class, null);
+ }
+
+ if (swingComponent != null) {
+ swingComponent.putClientProperty(SwingAdapter.class, this);
+ }
+
+ this.swingComponent = swingComponent;
+
+ swingAdapterListeners.swingComponentChanged(this, previousSwingComponent);
+ }
+ }
+
+ public ListenerList<SwingAdapterListener> getSwingAdapterListeners() {
+ return swingAdapterListeners;
+ }
+}
+
+class SwingAdapterRepaintManager extends RepaintManager {
+ public void addDirtyRegion(JComponent component, int x, int y, int width, int height) {
+ super.addDirtyRegion(component, x, y, width, height);
+
+ SwingAdapter swingAdapter = (SwingAdapter)component.getClientProperty(SwingAdapter.class);
+ if (swingAdapter != null) {
+ swingAdapter.repaint(x, y, width, height);
+ }
+ }
+}
+
+class SwingAdapterKeyboardFocusManager extends DefaultKeyboardFocusManager {
+ // TODO
+}
+
Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapterListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapterListener.java?rev=965680&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapterListener.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/SwingAdapterListener.java Mon Jul 19 23:45:36 2010
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+package org.apache.pivot.wtk;
+
+import javax.swing.JComponent;
+
+/**
+ * Swing adapter listener interface.
+ */
+public interface SwingAdapterListener {
+ /**
+ * Called when a Swing adapter's Swing component has changed.
+ *
+ * @param swingAdapter
+ * @param previousSwingComponent
+ */
+ public void swingComponentChanged(SwingAdapter swingAdapter, JComponent previousSwingComponent);
+}
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java?rev=965680&r1=965679&r2=965680&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Theme.java Mon Jul 19 23:45:36 2010
@@ -35,6 +35,7 @@ import org.apache.pivot.wtk.skin.PanelSk
import org.apache.pivot.wtk.skin.ScrollPaneSkin;
import org.apache.pivot.wtk.skin.SeparatorSkin;
import org.apache.pivot.wtk.skin.StackPaneSkin;
+import org.apache.pivot.wtk.skin.SwingAdapterSkin;
import org.apache.pivot.wtk.skin.TablePaneFillerSkin;
import org.apache.pivot.wtk.skin.TablePaneSkin;
import org.apache.pivot.wtk.skin.TextAreaSkin;
@@ -93,6 +94,7 @@ public abstract class Theme {
componentSkinMap.put(ScrollPane.class, ScrollPaneSkin.class);
componentSkinMap.put(Separator.class, SeparatorSkin.class);
componentSkinMap.put(StackPane.class, StackPaneSkin.class);
+ componentSkinMap.put(SwingAdapter.class, SwingAdapterSkin.class);
componentSkinMap.put(TablePane.class, TablePaneSkin.class);
componentSkinMap.put(TablePane.Filler.class, TablePaneFillerSkin.class);
componentSkinMap.put(TextArea.class, TextAreaSkin.class);
Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/SwingAdapterSkin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/SwingAdapterSkin.java?rev=965680&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/SwingAdapterSkin.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/SwingAdapterSkin.java Mon Jul 19 23:45:36 2010
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+package org.apache.pivot.wtk.skin;
+
+import java.awt.Graphics2D;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+
+import javax.swing.JComponent;
+
+import org.apache.pivot.wtk.ApplicationContext;
+import org.apache.pivot.wtk.Component;
+import org.apache.pivot.wtk.Dimensions;
+import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.Keyboard;
+import org.apache.pivot.wtk.Mouse;
+import org.apache.pivot.wtk.SwingAdapter;
+import org.apache.pivot.wtk.SwingAdapterListener;
+
+/**
+ * Swing adapter skin.
+ */
+public class SwingAdapterSkin extends ComponentSkin implements SwingAdapterListener {
+ @Override
+ public void install(Component component) {
+ super.install(component);
+
+ SwingAdapter swingAdapter = (SwingAdapter)component;
+ swingAdapter.getSwingAdapterListeners().add(this);
+ }
+
+ @Override
+ public int getPreferredWidth(int height) {
+ return getPreferredSize().width;
+ }
+
+ @Override
+ public int getPreferredHeight(int width) {
+ return getPreferredSize().height;
+ }
+
+ @Override
+ public Dimensions getPreferredSize() {
+ SwingAdapter swingAdapter = (SwingAdapter)getComponent();
+ JComponent swingComponent = swingAdapter.getSwingComponent();
+
+ int preferredWidth = 0;
+ int preferredHeight = 0;
+ if (swingComponent != null) {
+ java.awt.Dimension preferredSize = swingComponent.getPreferredSize();
+ preferredWidth = preferredSize.width;
+ preferredHeight = preferredSize.height;
+ }
+
+ return new Dimensions(preferredWidth, preferredHeight);
+ }
+
+ @Override
+ public void layout() {
+ SwingAdapter swingAdapter = (SwingAdapter)getComponent();
+ JComponent swingComponent = swingAdapter.getSwingComponent();
+
+ if (swingComponent != null) {
+ swingComponent.setSize(getWidth(), getHeight());
+ }
+ }
+
+ @Override
+ public void paint(Graphics2D graphics) {
+ SwingAdapter swingAdapter = (SwingAdapter)getComponent();
+ JComponent swingComponent = swingAdapter.getSwingComponent();
+
+ if (swingComponent != null) {
+ swingComponent.paint(graphics);
+ }
+ }
+
+ @Override
+ public boolean mouseMove(Component component, int x, int y) {
+ return processMouseEvent(component, MouseEvent.MOUSE_MOVED, x, y);
+ }
+
+ @Override
+ public void mouseOver(Component component) {
+ processMouseEvent(component, MouseEvent.MOUSE_ENTERED, 0, 0); // TODO
+ }
+
+ @Override
+ public void mouseOut(Component component) {
+ processMouseEvent(component, MouseEvent.MOUSE_EXITED, 0, 0); // TODO
+ }
+
+ @Override
+ public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
+ return processMouseEvent(component, MouseEvent.MOUSE_PRESSED, x, y);
+ }
+
+ @Override
+ public boolean mouseUp(Component component, Mouse.Button button, int x, int y) {
+ return processMouseEvent(component, MouseEvent.MOUSE_RELEASED, x, y);
+ }
+
+ @Override
+ public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
+ return processMouseEvent(component, MouseEvent.MOUSE_CLICKED, x, y);
+ }
+
+ @Override
+ public boolean mouseWheel(Component component, Mouse.ScrollType scrollType,
+ int scrollAmount, int wheelRotation, int x, int y) {
+ // TODO
+ return false;
+ }
+
+ @Override
+ public boolean keyTyped(Component component, char character) {
+ return processKeyEvent(component);
+ }
+
+ @Override
+ public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
+ return processKeyEvent(component);
+ }
+
+ @Override
+ public boolean keyReleased(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
+ return processKeyEvent(component);
+ }
+
+ @Override
+ public void enabledChanged(Component component) {
+ // TODO? User input events won't get fired, but Swing component may have
+ // focus, so we need to clear it.
+ }
+
+ @Override
+ public void focusedChanged(Component component, Component obverseComponent) {
+ // TODO?
+ }
+
+ private boolean processMouseEvent(Component component, int id, int x, int y) {
+ boolean consumed = false;
+
+ SwingAdapter swingAdapter = (SwingAdapter)getComponent();
+ JComponent swingComponent = swingAdapter.getSwingComponent();
+
+ if (swingComponent != null) {
+ Display display = component.getDisplay();
+ ApplicationContext.DisplayHost displayHost = display.getDisplayHost();
+ MouseEvent currentAWTEvent = (MouseEvent)displayHost.getCurrentAWTEvent();
+
+ if (currentAWTEvent != null) {
+ MouseEvent mouseEvent = new MouseEvent(swingComponent, id,
+ currentAWTEvent.getWhen(),
+ currentAWTEvent.getModifiers(),
+ x, y,
+ currentAWTEvent.getXOnScreen(),
+ currentAWTEvent.getYOnScreen(),
+ currentAWTEvent.getClickCount(),
+ currentAWTEvent.isPopupTrigger(),
+ currentAWTEvent.getButton());
+
+ swingComponent.dispatchEvent(mouseEvent);
+
+ consumed = mouseEvent.isConsumed();
+ }
+ }
+
+ return consumed;
+ }
+
+ private boolean processKeyEvent(Component component) {
+ boolean consumed = false;
+
+ SwingAdapter swingAdapter = (SwingAdapter)getComponent();
+ JComponent swingComponent = swingAdapter.getSwingComponent();
+
+ if (swingComponent != null) {
+ Display display = component.getDisplay();
+ ApplicationContext.DisplayHost displayHost = display.getDisplayHost();
+ KeyEvent currentAWTEvent = (KeyEvent)displayHost.getCurrentAWTEvent();
+
+ if (currentAWTEvent != null) {
+ KeyEvent keyEvent = new KeyEvent(swingComponent,
+ currentAWTEvent.getID(),
+ currentAWTEvent.getWhen(),
+ currentAWTEvent.getModifiers(),
+ currentAWTEvent.getKeyCode(),
+ currentAWTEvent.getKeyChar(),
+ currentAWTEvent.getKeyLocation());
+
+ swingComponent.dispatchEvent(keyEvent);
+
+ consumed = keyEvent.isConsumed();
+ }
+ }
+
+ return consumed;
+ }
+
+ @Override
+ public void swingComponentChanged(SwingAdapter swingAdapter, JComponent previousSwingComponent) {
+ invalidateComponent();
+ }
+}