📄 codecompletiondialog.java
字号:
package org.jawin.browser.dialog;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import org.jawin.browser.codecompletion.*;
import org.jawin.browser.log.Log;
/**
* Handles displaying a code completion dialog for the XSL editor panel
*
* <p>Title: Jawin Code Generation GUI</p>
* <p>Description: GUI for exploring type libraries and generating Java code</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: Open Source Incentive</p>
*
* @author Josh Passenger
* @version 1.0
*/
public class CodeCompletionDialog extends JDialog implements DocumentListener, KeyListener
{
public static final String TAG_STRING = "<xsl";
public static final String COLON = ":";
private JScrollPane scrollPane = new JScrollPane();
private CodeCompletionList list = new CodeCompletionList();
private JTextArea textArea = null;
private int TAG_LENGTH = TAG_STRING.length();
private int COLON_LENGTH = COLON.length();
private int caretPos = 0;
private int startPos = 0;
private StringBuffer typedBuffer = new StringBuffer();
private boolean codeSelected = false;
public CodeCompletionDialog()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public CodeCompletionDialog(Frame parent, String title, JTextArea newTextArea)
{
super(parent, title, false);
textArea = newTextArea;
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void insertUpdate(DocumentEvent e)
{
try
{
int offset = e.getOffset();
int length = e.getLength();
caretPos = offset + 1;
/**
* If this insert is too long just return
*/
if (length > COLON_LENGTH)
{
return;
}
String insertedText = e.getDocument().getText(offset, length);
/**
* Check what was inserted
*/
if (insertedText == null || !insertedText.equals(COLON))
{
return;
}
if (COLON_LENGTH > offset)
{
return;
}
String currentText = textArea.getText();
String compare = currentText.substring(offset - TAG_LENGTH, offset);
if (compare.equals(TAG_STRING))
{
Point caretPoint = textArea.getCaret().getMagicCaretPosition();
Point xslLocation = textArea.getLocationOnScreen();
Point popupPoint = new Point
(
(int) (caretPoint.getX() + xslLocation.getX()) + 10,
(int) (caretPoint.getY() + xslLocation.getY()) + 15
);
checkLocation(popupPoint);
setLocation(popupPoint);
show();
}
}
catch (Exception ex)
{
Log.getInstance().exception("CodeCompletionDialog.insertUpdate() failure on checking inserted text for showing dialog", ex);
}
}
/**
* Make sure the dialog does not appear off the screen
*
* @param point
*/
private void checkLocation(Point point)
{
int w = getWidth();
int h = getHeight();
int pointX = (int) point.getX();
int pointY = (int) point.getY();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) screenSize.getWidth();
int screenHeight = (int) screenSize.getHeight();
if (w + pointX >= screenWidth)
{
pointX = pointX - w - 10;
}
if (h + pointY >= screenHeight)
{
pointY = pointY - h - 15;
}
point.setLocation(pointX, pointY);
}
public void show()
{
startPos = textArea.getCaretPosition() + 1;
typedBuffer.setLength(0);
super.show();
list.requestFocus();
filterCodes();
codeSelected = false;
}
private void filterCodes()
{
String current = typedBuffer.toString();
list.clearSelection();
CodeCompletionListModel model = list.getCodeCompletionListModel();
for (int i = 0; i < model.getSize(); i++)
{
CodeCompletionConfig config = model.getConfigAt(i);
if (config.getName().startsWith(current))
{
list.setSelectedIndex(i);
list.ensureIndexIsVisible(i);
break;
}
}
}
public void removeUpdate(DocumentEvent e)
{
}
public void changedUpdate(DocumentEvent e)
{
}
public void keyTyped(KeyEvent e)
{
char typedChar = e.getKeyChar();
switch (typedChar)
{
case KeyEvent.VK_SPACE:
{
addString("" + typedChar);
dispose();
break;
}
case KeyEvent.VK_ENTER:
{
/**
* Check to see if a code has been selected
*/
int selectedIndex = list.getSelectedIndex();
if (selectedIndex > -1)
{
codeSelected((CodeCompletionConfig) list.getSelectedValue());
}
break;
}
case KeyEvent.VK_BACK_SPACE:
{
if (typedBuffer.length() == 0)
{
dispose();
break;
}
removeChar();
break;
}
case KeyEvent.VK_UP:
{
break;
}
case KeyEvent.VK_DOWN:
{
break;
}
case KeyEvent.VK_LEFT:
{
break;
}
case KeyEvent.VK_RIGHT:
{
break;
}
case KeyEvent.VK_ESCAPE:
{
dispose();
break;
}
default:
{
addString("" + typedChar);
}
}
filterCodes();
}
private void removeChar()
{
int pos = textArea.getCaretPosition();
StringBuffer buffer = new StringBuffer(textArea.getText());
buffer.replace(pos - 1, pos, "");
textArea.setText(buffer.toString());
pos -= 1;
textArea.setCaretPosition(pos);
typedBuffer.setLength(typedBuffer.length() - 1);
}
private void registerListeners()
{
list.addKeyListener(this);
list.addMouseListener(new MouseListener()
{
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
listDoubleClicked();
}
}
});
textArea.getDocument().addDocumentListener(this);
}
private void listDoubleClicked()
{
int index = list.getSelectedIndex();
if (index == -1)
{
return;
}
CodeCompletionConfig config = (CodeCompletionConfig) list.getSelectedValue();
codeSelected(config);
}
private void codeSelected(CodeCompletionConfig config)
{
if (codeSelected)
{
return;
}
codeSelected = true;
StringBuffer buffer = new StringBuffer(textArea.getText());
buffer.replace(startPos, startPos + typedBuffer.length(), "");
buffer.insert(startPos, config.getCleanCode());
textArea.setText(buffer.toString());
textArea.setCaretPosition(startPos + config.getOffset());
dispose();
}
private void addString(String newString)
{
int pos = textArea.getCaretPosition();
StringBuffer buffer = new StringBuffer(textArea.getText());
buffer.insert(pos, newString);
textArea.setText(buffer.toString());
pos += newString.length();
textArea.setCaretPosition(pos);
typedBuffer.append(newString);
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
private void jbInit() throws Exception
{
setSize(200, 300);
getContentPane().add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(list, null);
list.setRequestFocusEnabled(true);
list.requestDefaultFocus();
registerListeners();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -