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.editors.actionsequence.pages;
014:
015: import org.dom4j.Document;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.dialogs.MessageDialog;
018: import org.eclipse.jface.resource.JFaceResources;
019: import org.eclipse.jface.window.Window;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.events.SelectionListener;
023: import org.eclipse.swt.layout.FillLayout;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.swt.widgets.MessageBox;
030: import org.eclipse.ui.editors.text.TextEditor;
031: import org.pentaho.actionsequence.dom.ActionSequenceDocument;
032: import org.pentaho.actionsequence.dom.IActionSequenceElement;
033: import org.pentaho.designstudio.controls.WidgetFactory;
034: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceDocumentProvider;
035: import org.pentaho.designstudio.editors.actionsequence.BrokenParamDialog;
036: import org.pentaho.designstudio.editors.xmleditor.ColorManager;
037: import org.pentaho.designstudio.editors.xmleditor.IXMLDocumentProvider;
038: import org.pentaho.designstudio.editors.xmleditor.XMLConfiguration;
039: import org.pentaho.designstudio.editors.xmleditor.XMLDocumentProvider;
040: import org.pentaho.designstudio.messages.Messages;
041:
042: public class XMLPage extends TextEditor {
043:
044: private XMLDocumentProvider docProvider;
045: private ColorManager colorManager;
046: protected Composite editorPageComposite;
047: private Label headerLabel = null;
048:
049: public XMLPage() {
050: super ();
051: colorManager = new ColorManager();
052: setSourceViewerConfiguration(new XMLConfiguration(colorManager));
053: docProvider = createDocumentProvider();
054: setDocumentProvider(docProvider);
055: }
056:
057: public XMLDocumentProvider createDocumentProvider() {
058: return new ActionSequenceDocumentProvider();
059: }
060:
061: public String getHeaderText() {
062: return (Messages
063: .getString("ActionSequenceXMLEditor.UI_XML_PAGE_HEADER_TEXT")); //$NON-NLS-1$
064: }
065:
066: public void doSave(IProgressMonitor progressMonitor) {
067: boolean result = true;
068: if (checkDocument()) {
069: ActionSequenceDocument actionSequenceDocument = new ActionSequenceDocument(
070: getDocument());
071: IActionSequenceElement[] brokenReferences = actionSequenceDocument
072: .getBrokenReferences();
073: if (brokenReferences.length > 0) {
074: BrokenParamDialog brokenParamDialog = new BrokenParamDialog(
075: getEditorSite().getShell(), brokenReferences);
076: result = brokenParamDialog.open() == Window.OK;
077: ;
078: }
079: if (result) {
080: super .doSave(progressMonitor);
081: } else {
082: progressMonitor.setCanceled(true);
083: }
084: } else {
085: progressMonitor.setCanceled(true);
086: }
087: }
088:
089: public void doSaveAs() {
090: boolean result = true;
091: try {
092: ActionSequenceDocument actionSequenceDocument = new ActionSequenceDocument(
093: getDocument());
094: IActionSequenceElement[] brokenReferences = actionSequenceDocument
095: .getBrokenReferences();
096: if (brokenReferences.length > 0) {
097: BrokenParamDialog brokenParamDialog = new BrokenParamDialog(
098: getEditorSite().getShell(), brokenReferences);
099: result = brokenParamDialog.open() == Window.OK;
100: ;
101: }
102: } catch (Exception ex) {
103: result = MessageDialog
104: .openQuestion(
105: getEditorSite().getShell(),
106: Messages
107: .getString("ActionSequenceXMLEditor.SAVE_AS_DIALOG_TITLE"), Messages.getString("ActionSequenceXMLEditor.XML_ERROR_OCCURRED") + ex.toString() + "\n\n" + Messages.getString("ActionSequenceXMLEditor.CONTINUE_SAVE_OPERATION")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
108: }
109: if (result) {
110: if (checkDocument()) {
111: super .doSaveAs();
112: }
113: }
114: }
115:
116: public void dispose() {
117: colorManager.dispose();
118: super .dispose();
119: }
120:
121: public void refresh() {
122: docProvider.syncChanges();
123: }
124:
125: public Document getDocument() {
126: return (docProvider.getDocument());
127: }
128:
129: public void setChanged() {
130: docProvider.xmlChanged();
131: }
132:
133: public void prettyPrint() {
134: if (!checkDocument()) {
135: return;
136: }
137: docProvider.prettyPrint();
138: }
139:
140: public boolean checkDocument() {
141: String msg = docProvider.getDocErrors();
142: if (msg != null) {
143: MessageBox errMsg = new MessageBox(getEditorSite()
144: .getShell(), SWT.ICON_ERROR);
145: errMsg
146: .setText(Messages
147: .getString("BaseXMLEditor.ERROR_0001_INVALID_XML_TEXT")); //$NON-NLS-1$
148: errMsg
149: .setMessage(msg
150: + Messages
151: .getString("BaseXMLEditor.ERROR_0002_INVALID_XML_MESSAGE")); //$NON-NLS-1$
152: errMsg.open();
153: return (false);
154: }
155: refresh();
156: return (true);
157: }
158:
159: public IXMLDocumentProvider getXMLDocumentProvider() {
160: return (docProvider);
161: }
162:
163: public void createPartControl(Composite parent) {
164: editorPageComposite = WidgetFactory.createComposite(parent);
165: GridLayout gridLayout = new GridLayout(2, false);
166: gridLayout.horizontalSpacing = 30;
167: editorPageComposite.setLayout(gridLayout);
168:
169: headerLabel = WidgetFactory.createLabel(editorPageComposite,
170: getHeaderText(), SWT.BOLD);
171: headerLabel.setFont(JFaceResources.getHeaderFont());
172:
173: // The format Button
174: Button b = WidgetFactory.createButton(editorPageComposite,
175: "Format XML", SWT.PUSH); //$NON-NLS-1$
176: b.setAlignment(SWT.RIGHT);
177: b.setToolTipText(Messages
178: .getString("BaseXMLEditor.UI_FORMAT_XML_TOOLTIP")); //$NON-NLS-1$
179: GridData gridData = new GridData();
180: gridData.horizontalAlignment = SWT.BEGINNING;
181: b.setLayoutData(gridData);
182: b.addSelectionListener(new SelectionListener() {
183:
184: public void widgetSelected(SelectionEvent e) {
185: prettyPrint();
186: }
187:
188: public void widgetDefaultSelected(SelectionEvent e) {
189: prettyPrint();
190: }
191:
192: });
193:
194: Composite composite = WidgetFactory.createComposite(
195: editorPageComposite, SWT.BORDER);
196: gridData = new GridData(GridData.FILL_BOTH);
197: gridData.horizontalSpan = 2;
198: composite.setLayoutData(gridData);
199: composite.setLayout(new FillLayout());
200: super .createPartControl(composite);
201: }
202:
203: public static Composite createStretchComposite(Composite parent,
204: boolean grabHoriz, boolean grabVert, int marginSize) {
205: Composite composite = WidgetFactory.createComposite(parent);
206: GridLayout gridLayout = new GridLayout(1, true);
207: gridLayout.marginHeight = marginSize;
208: gridLayout.marginWidth = marginSize;
209:
210: composite.setLayout(gridLayout);
211: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
212: return (composite);
213: }
214: }
|