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 2009/06/25 16:48:17 UTC
svn commit: r788370 - in
/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json:
JSONViewer.java json_viewer.js json_viewer.wtkx
Author: gbrown
Date: Thu Jun 25 14:48:16 2009
New Revision: 788370
URL: http://svn.apache.org/viewvc?rev=788370&view=rev
Log:
Add drag/drop support to JSON Viewer.
Added:
incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.js
Modified:
incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/JSONViewer.java
incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.wtkx
Modified: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/JSONViewer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/JSONViewer.java?rev=788370&r1=788369&r2=788370&view=diff
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/JSONViewer.java (original)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/JSONViewer.java Thu Jun 25 14:48:16 2009
@@ -16,30 +16,32 @@
*/
package org.apache.pivot.tools.json;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pivot.collections.List;
import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.Sequence.Tree.Path;
+import org.apache.pivot.io.FileList;
import org.apache.pivot.serialization.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Clipboard;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
+import org.apache.pivot.wtk.DropAction;
import org.apache.pivot.wtk.Manifest;
import org.apache.pivot.wtk.Prompt;
import org.apache.pivot.wtk.TreeView;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtk.content.TreeBranch;
import org.apache.pivot.wtk.content.TreeNode;
-import org.apache.pivot.wtkx.WTKX;
import org.apache.pivot.wtkx.WTKXSerializer;
public class JSONViewer implements Application {
private Window window = null;
-
- @WTKX private TreeView treeView;
+ private TreeView treeView = null;
public static final String APPLICATION_KEY = "application";
@@ -50,7 +52,7 @@
wtkxSerializer.put(APPLICATION_KEY, this);
window = (Window)wtkxSerializer.readObject(this, "json_viewer.wtkx");
- wtkxSerializer.bind(this, JSONViewer.class);
+ treeView = (TreeView)wtkxSerializer.get("treeView");
window.open(display);
window.requestFocus();
@@ -78,30 +80,61 @@
if (clipboardContent != null
&& clipboardContent.containsText()) {
+ String json = null;
try {
- setJSON(clipboardContent.getText());
+ json = clipboardContent.getText();
+ setValue(JSONSerializer.parse(json));
} catch (IOException exception) {
Prompt.prompt(exception.getMessage(), window);
+ } catch(SerializationException exception) {
+ Prompt.prompt(exception.getMessage(), window);
}
}
}
- public void setJSON(String json) {
+ public DropAction drop(Manifest dragContent) {
+ DropAction dropAction = null;
+
try {
- Object value = JSONSerializer.parse(json);
+ FileList fileList = dragContent.getFileList();
+ if (fileList.getLength() == 1) {
+ File file = fileList.get(0);
+
+ JSONSerializer jsonSerializer = new JSONSerializer();
+ FileInputStream fileInputStream = null;
+ try {
+ try {
+ fileInputStream = new FileInputStream(file);
+ setValue(jsonSerializer.readObject(fileInputStream));
+ } finally {
+ if (fileInputStream != null) {
+ fileInputStream.close();
+ }
+ }
+ } catch (IOException exception) {
+ Prompt.prompt(exception.getMessage(), window);
+ } catch (SerializationException exception) {
+ Prompt.prompt(exception.getMessage(), window);
+ }
- if (value instanceof Map<?, ?>
- || value instanceof List<?>) {
- TreeBranch treeData = new TreeBranch();
- treeData.add(build(value));
- treeView.setTreeData(treeData);
- treeView.expandBranch(new Path(0));
+ dropAction = DropAction.COPY;
} else {
- Prompt.prompt("Clipboard does not contain a JSON object or array.", window);
+ Prompt.prompt("Multiple files not supported.", window);
}
- } catch (SerializationException exception) {
+ } catch(IOException exception) {
Prompt.prompt(exception.getMessage(), window);
}
+
+ return dropAction;
+ }
+
+ private void setValue(Object value) {
+ assert (value instanceof Map<?, ?>
+ || value instanceof List<?>);
+ TreeBranch treeData = new TreeBranch();
+ treeData.add(build(value));
+ treeView.setTreeData(treeData);
+ treeView.expandBranch(new Path(0));
}
@SuppressWarnings("unchecked")
Added: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.js
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.js?rev=788370&view=auto
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.js (added)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.js Thu Jun 25 14:48:16 2009
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+importPackage(java.lang);
+importPackage(org.apache.pivot.wtk);
+
+var treeViewDropTarget = new DropTarget() {
+ dragEnter: function(component, dragContent, supportedDropActions, userDropAction) {
+ var dropAction = null;
+
+ if (dragContent.containsFileList()
+ && DropAction.COPY.isSelected(supportedDropActions)) {
+ dropAction = DropAction.COPY;
+ }
+
+ return dropAction;
+ },
+
+ dragExit: function(component) {
+ },
+
+ dragMove: function(component, dragContent, supportedDropActions, x, y, userDropAction) {
+ return (dragContent.containsFileList() ? DropAction.COPY : null);
+ },
+
+ userDropActionChange: function(component, dragContent, supportedDropActions, x, y,
+ userDropAction) {
+ return (dragContent.containsFileList() ? DropAction.COPY : null);
+ },
+
+ drop: function(component, dragContent, supportedDropActions, x, y, userDropAction) {
+ var dropAction = null;
+
+ if (dragContent.containsFileList()) {
+ return application.drop(dragContent);
+ }
+
+ dragExit(component);
+
+ return dropAction;
+ }
+};
Modified: incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.wtkx?rev=788370&r1=788369&r2=788370&view=diff
==============================================================================
--- incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.wtkx (original)
+++ incubator/pivot/trunk/tools/src/org/apache/pivot/tools/json/json_viewer.wtkx Thu Jun 25 14:48:16 2009
@@ -20,11 +20,12 @@
xmlns:wtkx="http://pivot.apache.org/wtkx"
xmlns:content="org.apache.pivot.wtk.content"
xmlns="org.apache.pivot.wtk">
+ <wtkx:script src="json_viewer.js"/>
<content>
<ScrollPane horizontalScrollBarPolicy="fillToCapacity"
verticalScrollBarPolicy="fillToCapacity">
<view>
- <TreeView wtkx:id="treeView">
+ <TreeView wtkx:id="treeView" dropTarget="$treeViewDropTarget">
<nodeRenderer>
<content:TreeViewNodeRenderer showIcon="false"/>
</nodeRenderer>