alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.i18n.form;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.Position;
import javax.swing.text.StyledDocument;
import javax.swing.undo.UndoableEdit;

import org.netbeans.modules.form.*;
import org.netbeans.modules.form.RADConnectionPropertyEditor.RADConnectionDesignValue;
import org.netbeans.modules.i18n.HardCodedString;
import org.netbeans.modules.i18n.I18nString;
import org.netbeans.modules.i18n.I18nSupport;
import org.netbeans.modules.i18n.InfoPanel;
import org.netbeans.modules.i18n.java.JavaI18nString;
import org.netbeans.modules.i18n.java.JavaI18nSupport;

import org.openide.awt.UndoRedo;
import org.openide.cookies.EditorCookie;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.NotifyDescriptor;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;
import org.openide.text.NbDocument;
import org.openide.util.NbBundle;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;


/** 
 * Support for internationalizing strings in java sources with forms.
 *
 * @author Peter Zavadsky
 */
public class FormI18nSupport extends JavaI18nSupport {

    /** Variable for debug purposes. */
    private static final String DEBUG = "netbeans.debug.exceptions"; // NOI18N

    //see RADComponent and  RADVisualComponent
    private static final String RAD_PROPERTIES = "properties"; // NOI18N
    private static final String RAD_PROPERTIES2 = "properties2"; // NOI18N
    private static final String RAD_ACCESSIBILITY = "accessibility"; // NOI18N
    private static final String RAD_LAYOUT = "layout"; // NOI18N

// PENDING
// Until is used isGuardedPosition method, we can't listen on changes otherwise we got problems.
// This will be clearly possible when will be made possibility to detect guarded blocks via org.openide.text.NbDocument.GUARDED flag.
//    /** Listener which listens on changes of form document. */
//    DocumentListener documentListener;
    
    /** Constructor. */
    private FormI18nSupport(DataObject sourceDataObject) {
        super(sourceDataObject);
    }

    
    /** Creates I18nFinder. Overrides superclass method. */
    protected I18nFinder createFinder() {
        return new FormI18nFinder(sourceDataObject, document);
    }

    /** Creates I18nReplacer. Creates superclass method. */
    protected I18nReplacer createReplacer() {        
        return new FormI18nReplacer(/*sourceDataObject, document,*/ (FormI18nFinder)getFinder());
// PENDING
// See first PENDING.
//        documentListener = new DocumentListener() {
//            public void changedUpdate(DocumentEvent e) {
//            }

//            public void insertUpdate(DocumentEvent e) {
//                updateFormProperties();
//            }

//            public void removeUpdate(DocumentEvent e) {
//            }
//        };
        
//        document.addDocumentListener(documentListener);
    }
    
    /**
     * Overrides superclass methoid. 
     * Gets info panel about found hard string. */
    public JPanel getInfo(HardCodedString hcString) {
        return new FormInfoPanel(hcString, document);
    }
    
    /** Inner class for holding info about form proeprties which can include hardcoded string.
     * see formProperties variable in enclosing class. */
    private static class ValidFormProperty {
        /** Holds property of form. */
        private Node.Property property;
        
        /** Holds rad component name. */
        private String radCompName;
        
        /** How many occurences of found string should be skipped in this property.
         * 0 means find the first occurence.
         * All this just means that the property (mostly 'code generation-> pre-init, post-init etc.' properties)
         * could contain more than one occurence of found string
         * and in that case is very important to match and replace the same found in document. */
        private int skip;
   

        /** Constructor. */
        public ValidFormProperty(String radCompName, Node.Property property) {
            this.radCompName = radCompName;
            this.property    = property;
            this.skip        = 0;
        }

        /** Constructor. */
        public ValidFormProperty(ValidFormProperty validProperty) {
            radCompName = validProperty.getRADComponentName();
            property = validProperty.getProperty();
            skip = validProperty.getSkip();
        }
        
        
        /** Getter for radCompName property. */
        public String getRADComponentName() {
            return radCompName;
        }

        /** Getter for property.
         * @return property can contain hard-coded string */
        public Node.Property getProperty() {
            return property;
        }
        
        /** Getter for skip.
         * @return amount of occurences of hard-coded string to skip */
        public int getSkip() {
            return skip;
        }
        
        /** Increment the amount of occurences to skip. */
        public void incrementSkip() {
            skip++;
        }

        /** Decrement skip amount of occurences to skip. */
        public void decrementSkip() {
            if(skip > 0)
                skip--;
        }
        
    } // end of ValidFormProperty inner class
    
    /** Helper inner class for formProperties variable in enclosing class.
     * Provides sorting of ValidPropertyComparator classes with intenetion to get the order of
     * properties to match order like they are generated to initComponents form guarded block.
     * It has four stages of comparing two properies.
     * 1) the property which belongs to creation block (preCreationCode, customCreationCode, postCreationCode)
     *   is less (will be generated sooner) then property which is from init block(other names).
     * 2) than the property which component was added to form sooner is less then property which component was
     *   added later. (Top-level component is the least one.)
     * 3) than a) creation block: preCreationCode < (is less) customCreationCode < postCreationCode
     *         b) init block: preInitCode < set-method-properties < postInitCode
     * 4) than (for init block only) in case of set-method-properties. The property is less which has less index in
     *   array returned by method getAllProperties on component.
     * */
    private static class ValidFormPropertyComparator implements Comparator {
        
        private static final String CREATION_CODE_PRE    = "creationCodePre"; // NOI18N
        private static final String CREATION_CODE_CUSTOM = "creationCodeCustom"; // NOI18N
        private static final String CREATION_CODE_POST   = "creationCodePost"; // NOI18N
        
        private static final String INIT_CODE_PRE  = "initCodePre"; // NOI18N
        private static final String INIT_CODE_POST = "initCodePost"; // NOI18N

        /** FormModel on which FormDataObject the i18n session runs.*/
        private final FormModel formModel;
        
        
        /** Constructor. */
        public ValidFormPropertyComparator(FormDataObject formDataObject) {
            formModel = formDataObject.getFormEditor().getFormModel();
        }
        
        
        /** Compares two ValidFormPropertiesObjects. */
        public int compare(Object o1, Object o2) {
            // 1st stage
            String propName1 = ((ValidFormProperty)o1).getProperty().getName();
            String propName2 = ((ValidFormProperty)o2).getProperty().getName();
            
            boolean isInCreation1 = false;
            boolean isInCreation2 = false;
            
            if(propName1.equals(CREATION_CODE_PRE) || propName1.equals(CREATION_CODE_CUSTOM) || propName1.equals(CREATION_CODE_POST))
                isInCreation1 = true;
            
            if(propName2.equals(CREATION_CODE_PRE) || propName2.equals(CREATION_CODE_CUSTOM) || propName2.equals(CREATION_CODE_POST))
                isInCreation2 = true;
            
            if(isInCreation1 != isInCreation2)
                return isInCreation1 ? -1 : 1; // end of 1st stage
                
                // 2nd stage
                RADComponent comp1 = formModel.findRADComponent(((ValidFormProperty)o1).getRADComponentName());
                RADComponent comp2 = formModel.findRADComponent(((ValidFormProperty)o2).getRADComponentName());
                
                int index1 = -1;
                int index2 = -1;
                
                if(!comp1.equals(comp2)) {
                    Object[] components = formModel.getMetaComponents().toArray();
                    
                    for(int i=0; iisGuardedPosition(int) method only.
         * @see #isGuardedPosition */
        private boolean guardedPosition;


        /** Constructor. */
        public FormI18nFinder(DataObject sourceDataObject, StyledDocument document) {
            super(document);
            this.sourceDataObject = sourceDataObject;
            
            init();
        }
        
        
        /** Initializes finder. */
        private void init() {
            clearFormInfoValues();
            
            lastFoundProp = null;
            
            createFormProperties();
        }

        /** Resets finder. Overrides superclass method. */
        protected void reset() {
            super.reset();
            
            init();
        }
        
        /** Decrements skip value of last found property. Called from replacer. */
        void decrementLastFoundSkip() {
            if(lastFoundProp != null)
                lastFoundProp.decrementSkip();
        }

        /** Cretaes collection of properties of form which are value type of String.class
         * and could have null value (it saves time to determine the cases the value was changed).
         * Collection is referenced to formProperties variable.
         * @return True if sorted collection was created. */
        private synchronized boolean createFormProperties() {
            // creates new collection
            formProperties = new TreeSet(new ValidFormPropertyComparator((FormDataObject)sourceDataObject));
            updateFormProperties();

            return true;
        }

        /** Updates collection in formProperties variable. */
        private synchronized void updateFormProperties() {
            if(formProperties == null)
                return;

            // All components in current FormDataObject.
            Collection c = ((FormDataObject)sourceDataObject).getFormEditor().getFormModel().getMetaComponents();
            Iterator it = c.iterator();

            // search thru all RADComponents in the form
            while(it.hasNext()) {
                RADComponent radComponent = (RADComponent)it.next();
                Node.PropertySet[] propSets = radComponent.getProperties();

                // go thru properties sets
                for(int i=0; i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.