001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.controls;
014:
015: import java.io.ByteArrayInputStream;
016: import java.io.ByteArrayOutputStream;
017: import java.io.DataInputStream;
018: import java.io.DataOutputStream;
019: import java.io.IOException;
020:
021: import org.dom4j.Document;
022: import org.dom4j.DocumentHelper;
023: import org.eclipse.swt.dnd.ByteArrayTransfer;
024: import org.eclipse.swt.dnd.TransferData;
025: import org.pentaho.actionsequence.dom.AbstractIOElement;
026: import org.pentaho.actionsequence.dom.ActionOutput;
027: import org.pentaho.actionsequence.dom.ActionSequenceInput;
028: import org.pentaho.actionsequence.dom.ActionSequenceResource;
029:
030: /**
031: * An action definition input editor that is backed by an SWT check box button.
032: *
033: * @author Angelo Rodriguez
034: */
035:
036: public class ActionSequenceParamTransfer extends ByteArrayTransfer {
037:
038: private static ActionSequenceParamTransfer STATIC_INSTANCE = new ActionSequenceParamTransfer();
039: private static final String TYPE_NAME = "ActionSequenceParam"; //$NON-NLS-1$
040: private static final int TYPE_ID = registerType(TYPE_NAME);
041: private static int INPUT_ID = 0;
042: private static int OUTPUT_ID = 1;
043: private static int RESOURCE_ID = 2;
044:
045: private ActionSequenceParamTransfer() {
046: }
047:
048: public static ActionSequenceParamTransfer getInstance() {
049: return STATIC_INSTANCE;
050: }
051:
052: protected void javaToNative(Object object, TransferData transferData) {
053: if ((object instanceof ActionSequenceInput)
054: || (object instanceof ActionOutput)
055: || (object instanceof ActionSequenceResource)) {
056: if (isSupportedType(transferData)) {
057: try {
058: ByteArrayOutputStream out = new ByteArrayOutputStream();
059: DataOutputStream writeOut = new DataOutputStream(
060: out);
061: if (object instanceof ActionSequenceInput) {
062: writeOut.writeInt(INPUT_ID);
063: } else if (object instanceof ActionOutput) {
064: writeOut.writeInt(OUTPUT_ID);
065: } else if (object instanceof ActionSequenceResource) {
066: writeOut.writeInt(RESOURCE_ID);
067: }
068: byte[] buffer = ((AbstractIOElement) object)
069: .getElement().asXML().getBytes();
070: writeOut.writeInt(buffer.length);
071: writeOut.write(buffer);
072: buffer = out.toByteArray();
073: writeOut.close();
074: transferData.result = 0;
075: super .javaToNative(buffer, transferData);
076: } catch (IOException e) {
077:
078: }
079: }
080: }
081: }
082:
083: protected Object nativeToJava(TransferData transferData) {
084: AbstractIOElement result = null;
085: byte[] buffer = (byte[]) super .nativeToJava(transferData);
086: if (buffer != null) {
087: try {
088: ByteArrayInputStream in = new ByteArrayInputStream(
089: buffer);
090: DataInputStream readIn = new DataInputStream(in);
091: while (readIn.available() > 5) {
092: int id = readIn.readInt();
093: int size = readIn.readInt();
094: byte[] xml = new byte[size];
095: readIn.read(xml);
096: String xmlString = new String(xml);
097: Document document = DocumentHelper
098: .parseText(xmlString);
099: if (id == INPUT_ID) {
100: result = new ActionSequenceInput(document
101: .getRootElement(), null);
102: } else if (id == OUTPUT_ID) {
103: result = new ActionOutput(document
104: .getRootElement(), null);
105: } else if (id == RESOURCE_ID) {
106: result = new ActionSequenceResource(document
107: .getRootElement(), null);
108: }
109: }
110: readIn.close();
111: } catch (Exception ex) {
112: }
113: }
114: return result;
115: }
116:
117: protected int[] getTypeIds() {
118: return new int[] { TYPE_ID };
119: }
120:
121: protected String[] getTypeNames() {
122: return new String[] { TYPE_NAME };
123: }
124:
125: }
|