org.netbeans.modules.csl.api.SelectCodeElementAction Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.csl.api;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.List;
import java.util.MissingResourceException;
import javax.swing.Action;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.editor.BaseAction;
import org.netbeans.modules.csl.core.Language;
import org.netbeans.modules.csl.core.LanguageRegistry;
import org.netbeans.modules.csl.spi.ParserResult;
import org.netbeans.modules.parsing.api.ParserManager;
import org.netbeans.modules.parsing.api.ResultIterator;
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.parsing.api.UserTask;
import org.netbeans.modules.parsing.spi.ParseException;
import org.netbeans.modules.parsing.spi.Parser;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
/**
* Code selection according to syntax tree.
*
* TODO: javadoc selection
*
* @author Miloslav Metelka, Jan Pokorsky
* @deprecated use {@link CslActions#createSelectCodeElementAction(java.lang.String, boolean) } instead.
*/
@Deprecated
public final class SelectCodeElementAction extends BaseAction {
public static final String selectNextElementAction = "select-element-next"; //NOI18N
public static final String selectPreviousElementAction = "select-element-previous"; //NOI18N
private boolean selectNext;
/**
* Construct new action that selects next/previous code elements
* according to the language model.
*
*
* @param name name of the action (should be one of
*
* JavaKit.selectNextElementAction
* JavaKit.selectPreviousElementAction
* @param selectNext true
if the next element should be selected.
* False
if the previous element should be selected.
*/
public SelectCodeElementAction(String name, boolean selectNext) {
super(name);
this.selectNext = selectNext;
String desc = getShortDescription();
if (desc != null) {
putValue(SHORT_DESCRIPTION, desc);
}
}
public String getShortDescription(){
String name = (String)getValue(Action.NAME);
if (name == null) return null;
String shortDesc;
try {
shortDesc = NbBundle.getBundle(SelectCodeElementAction.class).getString(name); // NOI18N
}catch (MissingResourceException mre){
shortDesc = name;
}
return shortDesc;
}
public @Override void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
int selectionStartOffset = target.getSelectionStart();
int selectionEndOffset = target.getSelectionEnd();
if (selectionEndOffset > selectionStartOffset || selectNext) {
SelectionHandler handler = (SelectionHandler)target.getClientProperty(SelectionHandler.class);
if (handler == null) {
handler = new SelectionHandler(target);
target.addCaretListener(handler);
// No need to remove the listener above as the handler
// is stored is the client-property of the component itself
target.putClientProperty(SelectionHandler.class, handler);
}
if (selectNext) { // select next element
handler.selectNext();
} else { // select previous
handler.selectPrevious();
}
}
}
}
private static final class SelectionHandler extends UserTask implements CaretListener, Runnable {
private JTextComponent target;
private SelectionInfo[] selectionInfos;
private int selIndex = -1;
private boolean ignoreNextCaretUpdate;
SelectionHandler(JTextComponent target) {
this.target = target;
}
public void selectNext() {
if (selectionInfos == null) {
Source source = Source.create (target.getDocument());
try {
ParserManager.parse (Collections.