---
title: "php168主页、专题页模板制作工具"
author: "phantom"
date: "2009-03-20T16:07:45Z"
tags: []
slug: "php168"
draft: false
lastmod: "2009-03-20T16:10:21Z"
id: 352283
url: "https://phantom.iteye.com/blog/352283"
---

### javaVMEditor功能

在以php168模板模型的系统上制作专题、主页模板时可以快速切割页面代码，并生成相应文件入库。一旦自动入库的数据和要制作的页面的模板对应好，就可在cms上主页、专题管理中不用录入任何数据就可以预览到页面的全貌。

### javaVMEditor使用方法

 1 添加要制作的页面 ，记住ID  
 2 把要制作的页面的代码贴入javaVMEditor  
 3 在编辑工具中删除不需要的头尾代码  
 4 区分并选中要制成块的代码段，点工具的红、绿、蓝块中的一个  
 5 当把所有要制作的块选择制作完成后，点设置中的数据库设置,录入好数据库信息和页面ID  
 6 点文件保存  
 7 把生成好的文件打开，查看代码，把代码内容作为模板内容填写到模板管理系统中  
 8 设置要制作页面的模板为刚刚制作的模板  
 9. 预览页面完成制作  
 

### 工具下载

    [工具下载  3月19日更新  新增最终页制作](/admin/blogs/352283/uploads/Editor0.5.rar)    

    [源代码0.5](/admin/blogs/352283/uploads/Editor0.5.rar) 

java 写道

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
\* Copyright (c) 2000, 2004 IBM Corporation and others.  
\* All rights reserved. This program and the accompanying materials  
\* are made available under the terms of the Eclipse Public License v1.0  
\* which accompanies this distribution, and is available at  
\* http://www.eclipse.org/legal/epl-v10.html  
\*  
\* Contributors:  
\* IBM Corporation - initial API and implementation  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
package com.jh.app.texteditor;

import java.io.File;  
import java.io.IOException;  
import java.io.InputStream;

import org.eclipse.swt.SWT;  
import org.eclipse.swt.custom.StyleRange;  
import org.eclipse.swt.custom.StyledText;  
import org.eclipse.swt.events.DisposeEvent;  
import org.eclipse.swt.events.DisposeListener;  
import org.eclipse.swt.events.SelectionAdapter;  
import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.graphics.Color;  
import org.eclipse.swt.graphics.Font;  
import org.eclipse.swt.graphics.FontData;  
import org.eclipse.swt.graphics.Image;  
import org.eclipse.swt.graphics.ImageData;  
import org.eclipse.swt.graphics.Point;  
import org.eclipse.swt.graphics.RGB;  
import org.eclipse.swt.layout.GridData;  
import org.eclipse.swt.layout.GridLayout;  
import org.eclipse.swt.widgets.Display;  
import org.eclipse.swt.widgets.Event;  
import org.eclipse.swt.widgets.FileDialog;  
import org.eclipse.swt.widgets.FontDialog;  
import org.eclipse.swt.widgets.Menu;  
import org.eclipse.swt.widgets.MenuItem;  
import org.eclipse.swt.widgets.MessageBox;  
import org.eclipse.swt.widgets.Shell;  
import org.eclipse.swt.widgets.ToolBar;  
import org.eclipse.swt.widgets.ToolItem;

/\**  
\*/  
public class JavaVMEditor {  
private Image createStockImage(Display display, String path) {  
InputStream stream = JavaVMEditor.class.getResourceAsStream(path);  
ImageData imageData = new ImageData(stream);  
ImageData mask = imageData.getTransparencyMask();  
Image result = new Image(display, imageData, mask);  
try {  
stream.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
return result;  
}

public static void main(String\[] args) {  
Display display = new Display();  
JavaVMEditor example = new JavaVMEditor();  
Shell shell = example.open(display);

while (!shell.isDisposed())  
if (!display.readAndDispatch())  
display.sleep();  
display.dispose();  
}

private Color BLUE = null;

private FileDialog fileDialog;  
private Font font = null;  
private Color GREEN = null;

private Images images = new Images();  
private JavaLineStyler lineStyler = new JavaLineStyler();

private Color RED = null;  
private Shell shell;  
private StyledText text;  
private ToolBar toolBar;  
private SaveAction action = null;  
static private void setActionMethod(final Object obj, Menu menu,  
String actionName, final String mname ){  
setActionMethod(obj, menu, actionName, mname,new Object\[] {});  
}

static private void setActionMethod(final Object obj, Menu menu,  
String actionName, final String mname,final Object\[] args) {  
try {  
MenuItem item = new MenuItem(menu, SWT.PUSH);  
item.setText(actionName);  
item.addSelectionListener(new SelectionAdapter() {  
public void widgetSelected(SelectionEvent e) {

try {  
Util.privateMethodRun(obj.getClass(), mname, obj, args);  
} catch (Exception e1) {  
e1.printStackTrace();  
}  
}  
});

} catch (Exception e) {

e.printStackTrace();  
}  
}

private Menu createSetMenu() {  
Menu bar = shell.getMenuBar();  
Menu menu = new Menu(bar);  
setActionMethod(action, menu, "数据库设置", "initSet");  
setActionMethod(this, menu, "设置字体", "setFont");

return menu;  
}

private Menu createFileMenu() {  
Menu bar = shell.getMenuBar();  
Menu menu = new Menu(bar);  
setActionMethod(this, menu, "打开文件", "openFile");

setActionMethod(this, menu, "退出", "menuFileExit");  
return menu;  
}

private void createMenuBar() {  
Menu bar = new Menu(shell, SWT.BAR);  
shell.setMenuBar(bar);  
MenuItem fileItem2 = new MenuItem(bar, SWT.CASCADE);  
fileItem2.setText("文件");  
fileItem2.setMenu(createFileMenu());  
MenuItem editItem2 = new MenuItem(bar, SWT.CASCADE);  
editItem2.setText("设置");  
editItem2.setMenu(createSetMenu());  
createHelpMenu(bar);  
}

private void createHelpMenu(Menu parent) {  
Menu menu = new Menu(parent);  
MenuItem header = new MenuItem(parent, SWT.CASCADE);  
header.setText("帮助");  
header.setMenu(menu);

MenuItem item = new MenuItem(menu, SWT.PUSH);  
item.setText("关于");  
item.addSelectionListener(new SelectionAdapter() {  
public void widgetSelected(SelectionEvent e) {  
MessageBox box = new MessageBox(shell, SWT.ICON\_INFORMATION  
| SWT.OK);  
box.setText("模板代码生成器");  
box.setMessage("欢迎使用代码生成器V0.5");  
box.open();  
}  
});  
}

private void createShell(Display display) {  
shell = new Shell(display);  
shell.setText("Cms v9 模板制作工具 V0.5");  
images.loadAll(display);

shell.setImage(createStockImage(display, "generic\_example.gif"));  
GridLayout layout = new GridLayout();  
layout.numColumns = 1;  
shell.setLayout(layout);  
shell.addDisposeListener(new DisposeListener() {  
public void widgetDisposed(DisposeEvent e) {  
if (font != null)  
font.dispose();  
images.freeAll();  
RED.dispose();  
GREEN.dispose();  
BLUE.dispose();  
}  
});  
}

void createToolBar() {  
toolBar = new ToolBar(shell, SWT.NONE);  
new ToolItem(toolBar, SWT.SEPARATOR);

String menu\[] = {  
"切块",  
"标题",  
"作者",  
"责编",  
"时间",  
"来源",  
"URL",  
"栏目",  
"内容",  
"分页",  
"上一篇",  
"下一篇"  
};  
String menuAction\[] = {  
null,  
"$rsdb\[title]",  
"$rsdb\[author]",  
"$rsdb\[username]",  
"$rsdb\[posttime]",  
"$rsdb\[copyfrom]",  
"$rsdb\[copyfromurl]",  
"&lt;a href='list.php?fid={$rsdb\[fid]}'&gt;{$rsdb\[fname]}&lt;/a&gt;",  
"$rsdb\[content]",  
"$showpage",  
"\[&lt;a href=\\"bencandy.php?fid=$backdb\[fid]&id=$backdb\[aid]\\" onclick=\\"$backdb\[click]\\"&gt;上一篇&lt;/a&gt;]\\n&lt;a href=\\"bencandy.php?fid=$backdb\[fid]&id=$backdb\[aid]\\" onclick=\\"$backdb\[click]\\" title=\\"$backdb\[title]\\"&gt;$backdb\[subject]&lt;/a&gt;\\n",  
"\[&lt;a href=\\"bencandy.php?fid=$nextdb\[fid]&id=$nextdb\[aid]\\" onclick=\\"$nextdb\[click]\\"&gt;下一篇&lt;/a&gt;]\\n&lt;a href=\\"bencandy.php?fid=$nextdb\[fid]&id=$nextdb\[aid]\\" onclick=\\"$nextdb\[click]\\" title=\\"$nextdb\[title]\\"&gt;$nextdb\[subject]&lt;/a&gt;\\n" };  
for (int i = 0; i &lt; menu.length; i++) {  
cutBlockButton(toolBar, menu\[i], menuAction\[i]);  
}  
setActionMethod(action, toolBar, "生成专题页模板", "save");  
setActionMethod(action, toolBar, "生成最终页模板", "savelastpage");

}

private void setActionMethod(final Object obj, ToolBar toolBar,  
String actionName, final String mname) {  
try {  
ToolItem item = new ToolItem(toolBar, SWT.SEPARATOR);  
item = new ToolItem(toolBar, SWT.PUSH);  
item.setText(actionName);  
item.setToolTipText(actionName);  
item.addSelectionListener(new SelectionAdapter() {  
public void widgetSelected(SelectionEvent e) {

try {  
Util.privateMethodRun(obj.getClass(), mname, obj,  
new Object\[] {});  
} catch (Exception e1) {  
e1.printStackTrace();  
}  
}  
});

} catch (Exception e) {

e.printStackTrace();  
}

}

void cutBlockButton(ToolBar toolBar, String btName) {  
cutBlockButton(toolBar, btName, null);  
}

void cutBlockButton(ToolBar toolBar, String btName, final String txt) {  
ToolItem item = new ToolItem(toolBar, SWT.SEPARATOR);  
item = new ToolItem(toolBar, SWT.PUSH);  
item.setText(btName);  
item.setToolTipText(btName);  
item.addSelectionListener(new SelectionAdapter() {  
public void widgetSelected(SelectionEvent event) {  
if (txt == null) {  
cutblock(RED, null);  
} else {  
cutblock(BLUE, txt);  
}  
}  
});  
}

boolean flag = false;

private void cutblock(Color fg, String info) {  
text.removeLineStyleListener(lineStyler);

try {  
Point sel = text.getSelectionRange();  
String txt = text.getSelectionText();  
if (txt == null || txt.indexOf("#@start") &gt; -1  
|| txt.indexOf("#@end") &gt; -1) {  
msgbox("此处代码不适合切块！");  
return;  
}  
Event eventz = new Event();  
Point selection = text.getSelection();  
eventz.start = selection.x;  
eventz.end = selection.y;  
if (info == null) {  
eventz.text = "#@start@#" + txt + "#@end@#";  
} else {  
eventz.text = "#@start@#" + txt + "@var@" + info + "#@end@#";  
}  
try {  
Util.privateMethodRun(StyledText.class, "sendKeyEvent", text,  
new Object\[] { eventz });  
} catch (Exception e) {  
e.printStackTrace();  
}

if ((sel == null) || (sel.y == 0))  
return;  
text.setStyleRange(new StyleRange(sel.x, eventz.text.length(), fg,  
null, SWT.NORMAL));  
} catch (Exception e) {  
}

}

private void initializeColors() {  
Display display = Display.getDefault();  
RED = new Color(display, new RGB(255, 0, 0));  
BLUE = new Color(display, new RGB(0, 0, 255));  
GREEN = new Color(display, new RGB(0, 128, 0));  
}

void menuFileExit() {  
shell.close();  
}

private void msgbox(String msg) {  
MessageBox box = new MessageBox(shell, SWT.ICON\_INFORMATION | SWT.OK);  
box.setMessage(msg);  
box.open();  
}

private void createStyledText() {  
initializeColors();  
text = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.V\_SCROLL  
| SWT.H\_SCROLL);

GridData spec = new GridData();  
spec.horizontalAlignment = GridData.FILL;  
spec.grabExcessHorizontalSpace = true;  
spec.verticalAlignment = GridData.FILL;  
spec.grabExcessVerticalSpace = true;  
text.setLayoutData(spec);  
text.addLineStyleListener(lineStyler);

}

private Shell open(Display display) {  
createShell(display);  
action = new SaveAction(shell);  
createMenuBar();  
createToolBar();  
createStyledText();  
action.setText(text);  
shell.setSize(850, 650);  
shell.open();  
return shell;  
}

private void openFile(final StyledText text, String name) {  
final String textString;  
if ((name == null) || (name.length() == 0))  
return;  
File file = new File(name);

try {

textString = Util.readFile(file);  
} catch (IOException e) {  
String message = "文件" + file.getName() + "读写错误";  
msgbox(message);  
return;

} catch (Exception e) {  
String message = "文件" + file.getName() + "不存在";  
msgbox(message);  
return;  
}  
// Guard against superfluous mouse move events -- defer action until  
// later  
Display display = text.getDisplay();  
display.asyncExec(new Runnable() {  
public void run() {  
text.setText(textString);  
}  
});

}

void openFile() {  
if (fileDialog == null) {  
fileDialog = new FileDialog(shell, SWT.OPEN);  
}  
fileDialog.setFilterExtensions(new String\[] { "\*.vm", "\*.htm",  
"\*.html", "\*.\*" });  
String name = fileDialog.open();  
openFile(text, name);  
}

void setFont() {  
FontDialog fontDialog = new FontDialog(shell);  
fontDialog.setFontList((text.getFont()).getFontData());  
FontData fontData = fontDialog.open();  
if (fontData != null) {  
Font newFont = new Font(shell.getDisplay(), fontData);  
text.setFont(newFont);  
if (font != null)  
font.dispose();  
font = newFont;  
}  
}

}

全部代码见附件