📄 macroplugin.java
字号:
/* Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
* Copyright (C) 2001-2002 WOTLAS Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package wotlas.client.screen.plugin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import wotlas.utils.*;
import wotlas.libs.aswing.*;
import wotlas.libs.persistence.*;
import wotlas.common.*;
import wotlas.utils.Debug;
import wotlas.client.*;
import wotlas.client.screen.*;
/** Plug In to create/save/use HTML macros...
*
* @author Aldiss, Fred McMaster
*/
public class MacroPlugIn extends JPanelPlugIn {
/*------------------------------------------------------------------------------------*/
/** The name of the client macros config file.
*/
private final static String MACROS_PREFIX = "macro-";
private final static String MACROS_SUFFIX = ".cfg";
/** Max Number of macros the user can create.
*/
private final static int MAX_MACROS = 20;
/*------------------------------------------------------------------------------------*/
/** TextFields for macros
*/
private ATextField macrosFields[];
/** RadioButtons for macros
*/
private ARadioButton macrosRadios[];
/** JPanel for macros
*/
private JPanel macrosPanel[];
/** 'New' macro button.
*/
private AButton newMacroButton;
/** 'Use' macro button.
*/
private AButton useMacroButton;
/** 'Del' macro button.
*/
private AButton delMacroButton;
/** 'Save' macro button.
*/
private AButton saveMacroButton;
/** Center panel where the macros are set...
*/
private JPanel centerPanel;
/** Exclusive group of macros.
*/
private ButtonGroup macrosGroup;
/*------------------------------------------------------------------------------------*/
/** Constructor.
*/
public MacroPlugIn() {
super();
setLayout(new BorderLayout());
ATextArea taInfo = new ATextArea("You can create here HTML macros to use in the chat."
+" Each macro has a shortcut @N@ where N is the macro's index.");
taInfo.setLineWrap(true);
taInfo.setWrapStyleWord(true);
taInfo.setEditable(false);
taInfo.setAlignmentX(0.5f);
taInfo.setOpaque(false);
add(taInfo, BorderLayout.NORTH);
centerPanel = new JPanel( new GridLayout(MAX_MACROS,1,5,5) );
add(new JScrollPane(centerPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
),BorderLayout.CENTER);
macrosGroup = new ButtonGroup();
macrosFields = new ATextField[MAX_MACROS];
macrosRadios = new ARadioButton[MAX_MACROS];
macrosPanel = new JPanel[MAX_MACROS];
JPanel buttonsPanel = new JPanel(new GridLayout(1,4));
buttonsPanel.setBackground(Color.white);
add(buttonsPanel, BorderLayout.SOUTH);
ImageIcon im_newup = ClientDirector.getResourceManager().getImageIcon("new-mini-up.gif");
ImageIcon im_newdo = ClientDirector.getResourceManager().getImageIcon("new-mini-do.gif");
ImageIcon im_useup = ClientDirector.getResourceManager().getImageIcon("use-mini-up.gif");
ImageIcon im_usedo = ClientDirector.getResourceManager().getImageIcon("use-mini-do.gif");
ImageIcon im_delup = ClientDirector.getResourceManager().getImageIcon("del-mini-up.gif");
ImageIcon im_deldo = ClientDirector.getResourceManager().getImageIcon("del-mini-do.gif");
ImageIcon im_saveup = ClientDirector.getResourceManager().getImageIcon("save-mini-up.gif");
ImageIcon im_savedo = ClientDirector.getResourceManager().getImageIcon("save-mini-do.gif");
newMacroButton = new AButton(im_newup);
newMacroButton.setRolloverIcon(im_newdo);
newMacroButton.setPressedIcon(im_newdo);
newMacroButton.setBorderPainted(false);
newMacroButton.setContentAreaFilled(false);
newMacroButton.setFocusPainted(false);
newMacroButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
addMacro("");
}
});
useMacroButton = new AButton(im_useup);
useMacroButton.setRolloverIcon(im_usedo);
useMacroButton.setPressedIcon(im_usedo);
useMacroButton.setBorderPainted(false);
useMacroButton.setContentAreaFilled(false);
useMacroButton.setFocusPainted(false);
useMacroButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
String macro = getMacro( getSelectedIndex() );
ClientDirector.getDataManager().getClientScreen().getChatPanel().sendChatMessage(macro);
}
});
delMacroButton = new AButton(im_delup);
delMacroButton.setRolloverIcon(im_deldo);
delMacroButton.setPressedIcon(im_deldo);
delMacroButton.setBorderPainted(false);
delMacroButton.setContentAreaFilled(false);
delMacroButton.setFocusPainted(false);
delMacroButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
removeMacro( getSelectedIndex() );
}
});
saveMacroButton = new AButton(im_saveup);
saveMacroButton.setRolloverIcon(im_savedo);
saveMacroButton.setPressedIcon(im_savedo);
saveMacroButton.setBorderPainted(false);
saveMacroButton.setContentAreaFilled(false);
saveMacroButton.setFocusPainted(false);
saveMacroButton.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
save(getMacros());
}
});
buttonsPanel.add(newMacroButton);
buttonsPanel.add(useMacroButton);
buttonsPanel.add(delMacroButton);
buttonsPanel.add(saveMacroButton);
add(buttonsPanel, BorderLayout.SOUTH);
}
/*------------------------------------------------------------------------------------*/
/** Called once to initialize the plug-in.
* @return if true we display the plug-in, return false if something fails during
* this init(), this way the plug-in won't be displayed.
*/
public boolean init() {
String macros[] = load();
for( int i=0; i<macros.length; i++ )
addMacro( macros[i] );
return true; // this plug-in always works...
}
/*------------------------------------------------------------------------------------*/
/** Called when we need to reset the content of this plug-in.
*/
public void reset() {
// We remove the previous content
centerPanel.removeAll();
macrosGroup = new ButtonGroup();
for( int i=0; i<MAX_MACROS; i++ ) {
macrosFields[i] = null;
macrosRadios[i] = null;
macrosPanel[i] = null;
}
// and call init() again
init();
}
/*------------------------------------------------------------------------------------*/
/** To search & process a text with macros. A macro can not be used twice in a line.
* @param text text to parse for macros '@NN@' calls.
*/
public String processMacros( String text ) {
// A macro can not be used twice in
boolean macrosFound[] = new boolean[MAX_MACROS];
for( int i=0; i<MAX_MACROS; i++ )
macrosFound[i] = false;
do{
int pos=0, pos2=-1, id=-1;
// 1 - Search for the '@NN@' pattern.
do {
if(pos2+1==text.length()) return text;
pos=text.indexOf('@',pos2+1);
if(pos<0 || pos==text.length()-1) return text;
pos2=text.indexOf('@',pos+1);
if(pos2<0) return text;
if(pos2-pos>3) continue;
// Valid id ?
try{
id = Integer.parseInt(text.substring(pos+1,pos2));
}
catch(Exception e) {
//not a valid id
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -