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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.editor.options;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Map;
import javax.swing.KeyStroke;
import org.netbeans.editor.BaseKit;
import org.netbeans.editor.MultiKeyBinding;
import org.netbeans.modules.editor.java.JavaKit;
import org.netbeans.editor.ext.ExtKit;


/**
* Upgrade older options to the latest version
*
* @author Miloslav Metelka, Petr Nejedly
* @version 1.00
*/

class UpgradeOptions {
    
    /** Latest version of the options.
     * It is defined here so that only
     * this file gets modified
     * when there's a need for additional upgrade.
     */
    static final int LATEST_VERSION = 21;
    
    private static final String[] javaMapping = {
        "text", "java-whitespace", // NOI18N
        "identifier", "java-identifier", // NOI18N
        "float", "java-float-literal", // NOI18N
        "octal", "java-octal-literal", // NOI18N
        "long", "java-long-literal", // NOI18N
        "char", "java-char-literal", // NOI18N
        "double","java-double-literal", // NOI18N
        "line-comment", "java-line-comment", // NOI18N
        "operator", "java-operators", // NOI18N
        "string", "java-string-literal", // NOI18N
        "function", "java-layer-method", // NOI18N
        "keyword", "java-keywords", // NOI18N
        "block-comment", "java-block-comment", // NOI18N
        "error", "java-errors", // NOI18N
        "hex", "java-hex-literal", // NOI18N
        "int", "java-int-literal", // NOI18N
    };
    
    private static final String[] jspMapping = {
        
        "html-html-argument", "jsp-html-argument", // NOI18N
        "java-double", "jsp-java-double-literal", // NOI18N
        "html-html-text", "jsp-html-text", // NOI18N
        "java-float", "jsp-java-float-literal", // NOI18N
        "java-string", "jsp-java-string-literal", // NOI18N
        "java-function", "jsp-java-layer-method", // NOI18N
        "html-html-sgml-declaration", "jsp-html-sgml-declaration", // NOI18N
        "java-long", "jsp-java-long-literal", // NOI18N
        "java-block-comment", "jsp-java-block-comment", // NOI18N
        "html-html-operator", "jsp-html-operator", // NOI18N
        "html-html-value", "jsp-html-value", // NOI18N
        "java-text", "jsp-java-text", // NOI18N
        "java-operator", "jsp-java-operators", // NOI18N
        "java-char", "jsp-java-char-literal", // NOI18N
        "html-html-tag", "jsp-html-tag", // NOI18N
        "java-hex", "jsp-java-hex-literal", // NOI18N
        "text", "jsp-text", // NOI18N
        "java-line-comment", "jsp-java-line-comment", // NOI18N
        "html-html-ws", "jsp-html-ws", // NOI18N
        "html-html-sgml-comment", "jsp-html-sgml-comment", // NOI18N
        "java-int", "jsp-java-int-literal", // NOI18N
        "java-octal", "jsp-java-octal-literal", // NOI18N
        "html-html-block-comment", "jsp-html-block-comment", // NOI18N
        "html-html-error", "jsp-html-error", // NOI18N
        "java-keyword", "jsp-java-keywords", // NOI18N
        "html-html-character", "jsp-html-character", // NOI18N
        "java-identifier", "jsp-java-identifier", // NOI18N
    };
        
        
    static Map patchColorings( Class kitClass, Map oldColoring ) {
        if( kitClass.getName().equals( "org.netbeans.modules.editor.java.JavaKit" ) && // NOI18N
        ! oldColoring.containsKey( "java-keywords" ) // NOI18N
        ) return translateColoringNames( oldColoring, javaMapping  );

        if( kitClass.getName().equals( "org.netbeans.modules.web.core.syntax.JSPKit" ) && // NOI18N
        ! oldColoring.containsKey( "jsp-java-keywords" ) // NOI18N
        ) return translateColoringNames( oldColoring, jspMapping );

        return oldColoring;

    }
        
        
    private static Map translateColoringNames( Map oldMap, String[] nameMap ) {
        for( int i=0; i < nameMap.length; i += 2 ) {
            if( oldMap.containsKey( nameMap[i] ) ) {
                Object value = oldMap.get( nameMap[i] );
                oldMap.remove( nameMap[i]);
                oldMap.put( nameMap[i+1], value );
            }
        }
        return oldMap;
    }
        
        
    /** Upgrade the deserialized options.
     * @param options options to upgrade
     * @param version deserialized version of the options
     * @param latestVersion latest version of the options
     *   that will be set to them after they are upgraded
     */
    static void upgradeOptions(BaseOptions options, int version, int latestVersion) {
        if (options.getClass() == BaseOptions.class) { // upgrade global options
            if (version < 1) { // add VK_KP_??? shortcuts
                List kbl = options.getKeyBindingList();
                
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0), // keypad right
                        BaseKit.forwardAction
                    )
                );
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0), // keypad right
                        BaseKit.backwardAction
                    )
                );
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_KP_UP, 0), // keypad right
                        BaseKit.upAction
                    )
                );
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_KP_DOWN, 0), // keypad right
                        BaseKit.downAction
                    )
                );
                
                options.setKeyBindingList(kbl);
            }
        }
        
        // Check FastimportAction shortcut
        if (options.getClass() == JavaOptions.class) {
            if (version < 2) {
                List kbl = options.getKeyBindingList();
            
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_I,
                        InputEvent.ALT_MASK | InputEvent.SHIFT_MASK),
                        JavaKit.fastImportAction
                    )
                );

                options.setKeyBindingList(kbl);
            }
        }
            
        // Check macro shortcuts
        if (options.getClass() == JavaOptions.class) {
            if (version < 3) {
                List kbl = options.getKeyBindingList();
                
                kbl.add(
                new MultiKeyBinding(
                new KeyStroke[] {
                    KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_MASK),
                    KeyStroke.getKeyStroke(KeyEvent.VK_D, 0)
                },
                "macro-debug-var" // NOI18N
                )
                );
                
                options.setKeyBindingList(kbl);
            }
        }
        
        if (options.getClass() == BaseOptions.class) {
            if (version < 3) {
                List kbl = options.getKeyBindingList();
            
                
                kbl.add(
                    new MultiKeyBinding(
                        new KeyStroke[] {
                            KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_MASK),
                            KeyStroke.getKeyStroke(KeyEvent.VK_S, 0),
                        },
                        BaseKit.startMacroRecordingAction
                    )
                );

                kbl.add(
                    new MultiKeyBinding(
                        new KeyStroke[] {
                            KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_MASK),
                            KeyStroke.getKeyStroke(KeyEvent.VK_E, 0),
                        },
                        BaseKit.stopMacroRecordingAction
                    )
                );
                
                options.setKeyBindingList(kbl);
            }
            
        }
        
        // Check comment/uncomment shortcuts
        if (options.getClass() == JavaOptions.class) {
            if (version < 20) {
                List kbl = options.getKeyBindingList();
            
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK),
                        ExtKit.commentAction
                    )
                );

                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK),
                        ExtKit.uncommentAction
                    )
                );
                
                options.setKeyBindingList(kbl);
            }
        }
            
        
        // Check SHIFT+BACKSPACE shortcut
        if (options.getClass() == BaseOptions.class) {
            if (version < 21) {
                List kbl = options.getKeyBindingList();
            
                kbl.add(
                    new MultiKeyBinding(
                        KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, InputEvent.SHIFT_MASK),
                        BaseKit.deletePrevCharAction
                    )
                );
                
                options.setKeyBindingList(kbl);
            }
        }
                            
    }
        
}
... 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.