📄 descriptioneditordialog.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Common
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Berthold Daum
******************************************************************************/
package com.bdaum.jukebox;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.text.*;
import org.eclipse.jface.text.contentassist.*;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.*;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DescriptionEditorDialog extends TitleAreaDialog {
/**
* This class allows editing descriptions in a separate
* window. Key words starting with '$' and HTML tags are
* coded in a different color. In addition, a content
* assistant is implemented. This assistant is activated via
* Ctrl+Spacebar, and automatically after entering a '$'. In
* case of a '$' the content assistant makes proposals for
* keywords. If text is selected, the content assistant
* makes proposals for HTML character formatting. Also undo
* functions are implemented( Ctrl+Z for Undo, Ctrl+Y for
* Redo).
*/
public class KeywordCodeScanner extends RuleBasedScanner {
// This class implements a specific RuleBasedScanner.
// It assigns specific colors to keywords.
public KeywordCodeScanner() {
// We fetch the current Display instance
// for later retrieval of system colors
Display display = Display.getCurrent();
// We create a token for keywords and paint it green
IToken keyToken = new Token(new TextAttribute(display
.getSystemColor(SWT.COLOR_DARK_GREEN)));
// We create a token for HTML tags and paint it red
IToken htmlToken = new Token(new TextAttribute(display
.getSystemColor(SWT.COLOR_DARK_RED)));
// We only need a single rule to recognize a keyword
IRule[] rules = new IRule[2];
// By using a SingleLineRule we make sure that
// the keyword does not stretch across line breaks
rules[0] = new SingleLineRule("$", " ", keyToken, '\\');
rules[1] = new SingleLineRule("<", ">", htmlToken, '\\');
// We set this rule for the scanner
setRules(rules);
}
}
// All keywords
private final static String[] KEYWORDS = new String[]{
"performers", "producer", "publisher",
"pubDate", "title"};
// HTML style tags proposed for character formatting
private final static String[] HTMLTAGS = new String[]{"b",
"em", "i", "strong"};
// Display text for HTML style tags
private final static String[] STYLELABELS = new String[]{
"bold", "emphasis", "italic", "strong"};
public class KeywordContentAssistProcessor
implements
IContentAssistProcessor {
/**
* Compiles an array of CompletionProposal instances.
*
* @param viewer -
* The viewer, from which this method is called
* @param documentOffset -
* The current position in the document
*/
/**
* Make automatic proposals after a $-character
*/
public char[] getCompletionProposalAutoActivationCharacters() {
return new char[]{'$'};
}
public ICompletionProposal[] computeCompletionProposals(
ITextViewer viewer, int documentOffset) {
IDocument doc = viewer.getDocument();
// Get text selection
Point selectedRange = viewer.getSelectedRange();
List propList;
try {
propList = (selectedRange.y == 0)
? computeKeywordProposals(getQualifier(doc, documentOffset),
documentOffset)
: computeHtmlProposals(selectedRange.x, doc.get(
selectedRange.x, selectedRange.y));
// Convert into Array
return (CompletionProposal[])
propList.toArray(new CompletionProposal[propList.size()]);
} catch (BadLocationException e) {
return new CompletionProposal[0];
}
}
/**
* Compiles proposals for HTML mark-up.
*
* @param documentOffset -
* the current position in the text
* @param selectedText -
* the currently selected text
* @return - list of proposals
*/
private List computeHtmlProposals(int documentOffset,
String selectedText) {
List propList = new ArrayList();
for (int i = 0; i < HTMLTAGS.length; i++) {
// Compute the string that will replace the selection
String insert = "<" + HTMLTAGS[i] + ">" + selectedText
+ "</" + HTMLTAGS[i] + ">";
int cursor = insert.length();
// Construct proposal with replacement string and
// display label
CompletionProposal proposal = new CompletionProposal(
insert, documentOffset, selectedText.length(), cursor,
null, STYLELABELS[i], null, insert);
propList.add(proposal);
}
return propList;
}
/**
* Retrieves qualifying user input.
*
* @param viewer -
* The viewer under which we work
* @param documentOffset -
* The current position in the document
* @return String
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -