mail[Wesnoth-commits] r44025 - in /trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin: utils/ wizards/generator/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by timotei_cluj on July 09, 2010 - 00:08:
Author: timotei
Date: Fri Jul  9 00:08:43 2010
New Revision: 44025

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44025&view=rev
Log:
eclipse plugin: schema-based wizards - step 5
- generating the wml code
- inserting the wml in a new file or in edited file in the editor

Modified:
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/StringUtils.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/SchemaParser.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGenerator.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageKey.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageTag.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncher.java
    
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncherPage0.java

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/StringUtils.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/StringUtils.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/StringUtils.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/utils/StringUtils.java
 Fri Jul  9 00:08:43 2010
@@ -169,4 +169,16 @@
 
                return string.replace(source, 
ListUtils.concatenateArray(tmpTarget, "\n"));
        }
+
+       public static String multiples(String sequence, int times)
+       {
+               if (sequence == null)
+                       return null;
+               StringBuilder res = new StringBuilder(sequence.length() * 
times);
+               for (int i = 0; i < times; i++)
+               {
+                       res.append(sequence);
+               }
+               return res.toString();
+       }
 }

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/SchemaParser.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/SchemaParser.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/SchemaParser.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/SchemaParser.java
 Fri Jul  9 00:08:43 2010
@@ -30,6 +30,7 @@
 
        public void parseSchema(boolean force)
        {
+               //TODO: sort tags's keys by cardinality (required first) ??
                if (parsingDone_ && !force)
                        return;
 

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGenerator.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGenerator.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGenerator.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGenerator.java
 Fri Jul  9 00:08:43 2010
@@ -4,21 +4,23 @@
  */
 package wesnoth_eclipse_plugin.wizards.generator;
 
-import java.util.List;
+import org.eclipse.jface.wizard.IWizardPage;
 
+import wesnoth_eclipse_plugin.utils.StringUtils;
 import wesnoth_eclipse_plugin.wizards.NewWizardTemplate;
 import wesnoth_eclipse_plugin.wizards.WizardsConstants;
 
 public class WizardGenerator extends NewWizardTemplate
 {
-       List<WizardGeneratorPageKey>    pagesList_;
-       private String                                  tagName_;
+       private String  tagName_;
+       private byte    indent_;
 
-       public WizardGenerator(String title, String tagName) {
+       public WizardGenerator(String title, String tagName, byte indent) {
                SchemaParser.getInstance().parseSchema(false);
                setWindowTitle(title);
                Tag tagContent = 
SchemaParser.getInstance().getTags().get(tagName);
                tagName_ = tagName;
+               indent_ = indent;
 
                // keys section
                int keysNr = tagContent.KeyChildren.size();
@@ -27,13 +29,14 @@
                for (int i = 0; i < pgsKey; i++)
                {
                        tempPageKey = new WizardGeneratorPageKey(tagName, 
tagContent.KeyChildren, startKey,
-                                                                             
                  startKey + WizardsConstants.MaxTextBoxesOnPage);
+                                                                             
                  startKey + WizardsConstants.MaxTextBoxesOnPage, (byte) 
(indent_ + 1));
                        startKey += WizardsConstants.MaxTextBoxesOnPage;
                        addPage(tempPageKey);
                }
                if (keysNr - 1 > 0)
                {
-                       tempPageKey = new WizardGeneratorPageKey(tagName, 
tagContent.KeyChildren, startKey, keysNr - 1);
+                       tempPageKey = new WizardGeneratorPageKey(tagName, 
tagContent.KeyChildren,
+                                                                             
                  startKey, keysNr - 1, (byte) (indent_ + 1));
                        addPage(tempPageKey);
                }
 
@@ -44,13 +47,14 @@
                for (int i = 0; i < pgsTag; i++)
                {
                        tempPageTag = new WizardGeneratorPageTag(tagName, 
tagContent.TagChildren, startTag,
-                                       startTag + 
WizardsConstants.MaxGroupsOnPage);
+                                                                             
          startTag + WizardsConstants.MaxGroupsOnPage, (byte) (indent_ + 1));
                        startTag += WizardsConstants.MaxTextBoxesOnPage;
                        addPage(tempPageTag);
                }
                if (tagsNr - 1 > 0)
                {
-                       tempPageTag = new WizardGeneratorPageTag(tagName, 
tagContent.TagChildren, startTag, tagsNr - 1);
+                       tempPageTag = new WizardGeneratorPageTag(tagName, 
tagContent.TagChildren,
+                                                                             
          startTag, tagsNr - 1, (byte) (indent_ + 1));
                        addPage(tempPageTag);
                }
 
@@ -58,6 +62,11 @@
                {
                        addPage(new WizardGeneratorPage404(tagName));
                }
+       }
+
+       public byte getIndent()
+       {
+               return indent_;
        }
 
        @Override
@@ -70,13 +79,24 @@
        public boolean performFinish()
        {
                // logic
-
-               data_ = "temp";
-
+               String result = StringUtils.multiples("\t", indent_) + "[" + 
tagName_ + "]\n";
+               StringBuilder keys = new StringBuilder();
+               StringBuilder tags = new StringBuilder();
+               for (IWizardPage page : getPages())
+               {
+                       if (page instanceof WizardGeneratorPageKey)
+                               keys.append(((WizardGeneratorPageKey) 
page).getContent());
+                       else if (page instanceof WizardGeneratorPageTag)
+                               tags.append(((WizardGeneratorPageTag) 
page).getContent());
+                       else
+                               ; // skip 404 pages
+               }
+               result += (keys.toString() + tags.toString());
+               result += (StringUtils.multiples("\t", indent_) + "[/" + 
tagName_ + "]\n");
+               data_ = result;
                // for now let's just return tag's name
                objectName_ = tagName_;
                isFinished_ = true;
                return true;
        }
-
 }

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageKey.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageKey.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageKey.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageKey.java
 Fri Jul  9 00:08:43 2010
@@ -11,19 +11,26 @@
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
+
+import wesnoth_eclipse_plugin.utils.StringUtils;
 
 public class WizardGeneratorPageKey extends WizardPage
 {
        private List<TagKey>    keys_;
        private int                             startIndex_, endIndex_;
        private Composite               container_;
+       private byte                    indent_;
 
-       public WizardGeneratorPageKey(String tagName, List<TagKey> keys, int 
startIndex, int endIndex) {
+       public WizardGeneratorPageKey(String tagName, List<TagKey> keys,
+                       int startIndex, int endIndex, byte indent) {
                super("wizardPageKey" + startIndex);
                setTitle(tagName + " new wizard");
                //setDescription(String.format("page %d to %d out of %d", 
startIndex, endIndex, keys.size()));
+
+               indent_ = indent;
 
                startIndex_ = startIndex;
                endIndex_ = endIndex;
@@ -56,4 +63,18 @@
                }
                setPageComplete(true);
        }
+
+       public String getContent()
+       {
+               StringBuilder result = new StringBuilder();
+               for (Control child : container_.getChildren())
+               {
+                       if (!(child instanceof Text))
+                               continue;
+
+                       result.append(StringUtils.multiples("\t", indent_) +
+                                       child.getData("name") + "=" + ((Text) 
child).getText() + "\n");
+               }
+               return result.toString();
+       }
 }

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageTag.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageTag.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageTag.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardGeneratorPageTag.java
 Fri Jul  9 00:08:43 2010
@@ -6,6 +6,7 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Map.Entry;
 
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.swt.SWT;
@@ -19,6 +20,8 @@
 import org.eclipse.swt.widgets.List;
 
 import wesnoth_eclipse_plugin.utils.GUIUtils;
+import wesnoth_eclipse_plugin.utils.ListUtils;
+import wesnoth_eclipse_plugin.utils.StringUtils;
 import wesnoth_eclipse_plugin.wizards.WizardUtils;
 
 public class WizardGeneratorPageTag extends WizardPage
@@ -27,11 +30,15 @@
        private HashMap<String, java.util.List<String>> content_;
        private int                                                           
                  startIndex_, endIndex_;
        private Composite                                                     
          container_;
+       private byte                                                          
          indent_;
 
-       public WizardGeneratorPageTag(String tagName, java.util.List<Tag> 
tags, int startIndex, int endIndex) {
+       public WizardGeneratorPageTag(String tagName, java.util.List<Tag> 
tags,
+                       int startIndex, int endIndex, byte indent) {
                super("wizardPageTag" + startIndex);
                setTitle(tagName + " new wizard");
                //setDescription(String.format("page %d to %d out of %d", 
startIndex, endIndex, tags.size()));
+
+               indent_ = indent;
 
                startIndex_ = startIndex;
                endIndex_ = endIndex;
@@ -102,7 +109,8 @@
        private void addNewItem(List targetList, String tagName)
        {
                //TODO: check for multiple addings
-               WizardGenerator wizard = new WizardGenerator("Create a new " 
+ tagName, tagName);
+               WizardGenerator wizard =
+                               new WizardGenerator("Create a new " + 
tagName, tagName, (byte) (indent_ + 1));
                WizardUtils.launchWizard(wizard, getShell(), null);
                if (wizard.isFinished())
                {
@@ -122,4 +130,16 @@
                content_.get(tagName).remove(targetList.getSelectionIndex());
                targetList.remove(targetList.getSelectionIndex());
        }
+
+       public String getContent()
+       {
+               StringBuilder result = new StringBuilder();
+               for (Entry<String, java.util.List<String>> tag : 
content_.entrySet())
+               {
+                       result.append(StringUtils.multiples("\t", indent_) + 
"[" + tag.getKey() + "]\n");
+                       
result.append(ListUtils.concatenateList(tag.getValue(), "\n\t"));
+                       result.append(StringUtils.multiples("\t", indent_) + 
"[/" + tag.getKey() + "]\n");
+               }
+               return result.toString();
+       }
 }

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncher.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncher.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncher.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncher.java
 Fri Jul  9 00:08:43 2010
@@ -3,6 +3,31 @@
  *
  */
 package wesnoth_eclipse_plugin.wizards.generator;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
 
 import wesnoth_eclipse_plugin.wizards.NewWizardTemplate;
 import wesnoth_eclipse_plugin.wizards.WizardUtils;
@@ -11,6 +36,7 @@
 {
        WizardLauncherPage0     page0_;
        WizardLauncherPage1     page1_;
+       WizardGenerator         wizard_;
 
        public WizardLauncher() {
                setWindowTitle("Wizard launcher");
@@ -32,9 +58,107 @@
        @Override
        public boolean performFinish()
        {
-               WizardGenerator wizard = new 
WizardGenerator(page1_.getTagDescription() + " new wizard", 
page1_.getTagName());
-               WizardUtils.launchWizard(wizard, getShell(), selection_);
+               wizard_ = new WizardGenerator(page1_.getTagDescription() + " 
new wizard", page1_.getTagName(), (byte) 0);
+               WizardUtils.launchWizard(wizard_, getShell(), selection_);
 
-               return false;
+               IRunnableWithProgress op = new IRunnableWithProgress() {
+                       @Override
+                       public void run(IProgressMonitor monitor) throws 
InvocationTargetException
+                       {
+                               try
+                               {
+                                       doFinish(monitor);
+                               } catch (CoreException e)
+                               {
+                                       throw new 
InvocationTargetException(e);
+                               } finally
+                               {
+                                       monitor.done();
+                               }
+                       }
+               };
+               try
+               {
+                       getContainer().run(false, false, op);
+               } catch (InterruptedException e)
+               {
+                       return false;
+               } catch (InvocationTargetException e)
+               {
+                       Throwable realException = e.getTargetException();
+                       MessageDialog.openError(getShell(), "Error", 
realException.getMessage());
+                       return false;
+               }
+               return true;
+       }
+
+       private void doFinish(IProgressMonitor monitor) throws CoreException
+       {
+               // The file is opened in the editor -> just copy-paste the 
text
+               if (!(page0_.getIsTargetNewFile()))
+               {
+                       try
+                       {
+                               IEditorPart part = page0_.getEditedFile();
+                               if (!(part instanceof AbstractTextEditor))
+                                       return;
+                               ITextEditor editor = (ITextEditor) part;
+                               IDocumentProvider dp = 
editor.getDocumentProvider();
+                               IDocument doc = 
dp.getDocument(editor.getEditorInput());
+                               int offset = ((ITextSelection) 
editor.getSelectionProvider().getSelection()).getOffset();
+                               doc.replace(offset, 0, 
wizard_.getData().toString());
+
+                       } catch (Exception e)
+                       {
+                               e.printStackTrace();
+                       }
+                       return;
+               }
+               final String containerName = page0_.getDirectoryName();
+               final String fileName = page0_.getFileName();
+
+               // create the file
+               monitor.beginTask("Creating " + fileName, 10);
+               IWorkspaceRoot root = 
ResourcesPlugin.getWorkspace().getRoot();
+               IResource resource = root.findMember(new Path(containerName));
+
+               IContainer container = (IContainer) resource;
+               final IFile file = container.getFile(new Path(fileName));
+
+               try
+               {
+                       InputStream stream = new 
ByteArrayInputStream(wizard_.getData().toString().getBytes());
+
+                       if (file.exists())
+                       {
+                               file.setContents(stream, true, true, monitor);
+                       }
+                       else
+                       {
+                               file.create(stream, true, monitor);
+                       }
+
+                       stream.close();
+               } catch (Exception e)
+               {
+                       e.printStackTrace();
+               }
+
+               monitor.worked(5);
+               monitor.setTaskName("Opening file for editing...");
+               getShell().getDisplay().asyncExec(new Runnable() {
+                       @Override
+                       public void run()
+                                       {
+                                               IWorkbenchPage page = 
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+                                               try
+                                               {
+                                                       IDE.openEditor(page, 
file, true);
+                                               } catch (PartInitException e)
+                                               {
+                                               }
+                                       }
+               });
+               monitor.worked(5);
        }
 }

Modified: 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncherPage0.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncherPage0.java?rev=44025&r1=44024&r2=44025&view=diff
==============================================================================
--- 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncherPage0.java
 (original)
+++ 
trunk/utils/java/eclipse_plugin/src/wesnoth_eclipse_plugin/wizards/generator/WizardLauncherPage0.java
 Fri Jul  9 00:08:43 2010
@@ -21,9 +21,7 @@
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorReference;
-import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
 
 import wesnoth_eclipse_plugin.Activator;
@@ -201,32 +199,24 @@
                else
                {
                        // current file checking
-                       if (selection_ != null && selection_.isEmpty() == 
false &&
-                                       selection_ instanceof 
IStructuredSelection && selection_.size() > 0)
-                       {
-                               try
-                               {
-                                       IEditorReference[] references =
-                                                       
Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getEditorReferences();
-                                       if (references.length > 0)
-                                       {
-                                               IEditorInput input = 
references[0].getEditorInput();
-                                               
lblCurrentFileOpened.setText("File " + input.getName() + " opened.");
-                                       }
-                                       else
-                                       {
-                                               
lblCurrentFileOpened.setText("No file opened.");
-                                               setErrorMessage("No file 
opened.");
-                                               return;
-                                       }
-                               } catch (PartInitException e)
-                               {
-                                       e.printStackTrace();
-                               }
+                       if (getEditedFile() != null)
+                       {
+                               lblCurrentFileOpened.setText("File " + 
getEditedFile().getEditorInput().getName() + " opened.");
+                       }
+                       else
+                       {
+                               lblCurrentFileOpened.setText("No file 
opened.");
+                               setErrorMessage("No file opened.");
+                               return;
                        }
                }
                setPageComplete(true);
                setErrorMessage(null);
+       }
+
+       public IEditorPart getEditedFile()
+       {
+               return 
Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getPages()[0].getActiveEditor();
        }
 
        public void updateEnabledStatus()
@@ -277,7 +267,7 @@
        {
                ContainerSelectionDialog dialog =
                                new ContainerSelectionDialog(getShell(), 
ResourcesPlugin.getWorkspace().getRoot(), false,
-                                               "Select a campaign project");
+                                               "Select a directory");
                if (dialog.open() == ContainerSelectionDialog.OK)
                {
                        Object[] result = dialog.getResult();
@@ -290,11 +280,16 @@
 
        public String getFileName()
        {
-               return radioNewFile.getSelection() == true ? 
txtFileName_.getText() : "";
+               return radioNewFile.getSelection() == true ? 
txtFileName_.getText() : getEditedFile().getEditorInput().getName();
        }
 
        public String getDirectoryName()
        {
                return radioNewFile.getSelection() == true ? 
txtDirectory_.getText() : "";
        }
+
+       public boolean getIsTargetNewFile()
+       {
+               return radioNewFile.getSelection();
+       }
 }




Related Messages


Powered by MHonArc, Updated Fri Jul 09 01:00:09 2010