📄 importimagemap.java
字号:
/*
* ImportImageMap.java
* Heiko Schr鰀er
* wikiimagemapper.sourceforge.net
*
* 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 cn.dxm.client;
import java.awt.datatransfer.*;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.awt.Toolkit;
/**
* This class is used for import imagemap code, hold in clipboard, and create the defined shapes
*
* @author Heiko Schroeder <hainet@gmx.de>
*/
public class ImportImageMap {
/**
* Reads the actual clipboard content
*
* @return String with clipboard text
*/
private static String getClipboardContent()
{
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkSystemClipboardAccess();
}
catch (Exception e) {e.printStackTrace();}
}
Toolkit tk = Toolkit.getDefaultToolkit();
Clipboard systemClipboard = tk.getSystemClipboard();
Transferable transferData = systemClipboard.getContents(null);
try {
Object content = transferData.getTransferData( DataFlavor.stringFlavor );
return (String)content;
}catch ( UnsupportedFlavorException e ) {}
catch ( IOException e ) {}
return null;
}
/**
* processImageMapSyntax reads the actual clipboard content and parses it to
* import, previously exported imagemap code
*
* @return A list of shapes, defined in the clipboard-text
*/
public static List<Shape> processImageMapSyntax() {
List<Shape> shapes = new ArrayList<Shape>();
String clipboardContent = (String)getClipboardContent();
String[] clipboardRows = clipboardContent.split("\n");
for (int i=0; i<clipboardRows.length;i++)
{
System.out.println(i+": "+clipboardRows[i]);
Shape parsedShape = parseLine(clipboardRows[i]);
if (parsedShape != null)
shapes.add(parsedShape);
}
System.out.println("Number of parsed shapes: "+shapes.size());
return shapes;
}
/**
* parseLine, parses a line from the imagemap code and creates the descibed
* shape.
*
* @param a line of imagemap code (String)
* @param the shape, defined in that line of code
*/
public static Shape parseLine(String line)
{
String url = "";
String shape = "";
String alt = "";
String[] coords = null;
Shape shapeInstance = null;
AttributeBean shapeAttribute = new AttributeBean();
String[] lineSplitted = line.split(";"); // Split line into single attributes, seperated by ";"
// Parse line
if (lineSplitted.length > 1) { // line is not start,end or imagename
url = lineSplitted[0];
for (int i=1; i<lineSplitted.length; i++)
{
String[] token = lineSplitted[i].split("\"");
if (token.length > 1)
{
if(token[0].compareTo("shape=")==0)
shape = token[1];
else if (token[0].compareTo("alt=")==0)
alt = token[1];
else if (token[0].compareTo("coords=")==0)
coords = token[1].split(",");
else
System.out.println("Can not handle this: "+token[0]);
}
}
// Build the Shape object
if (coords != null){
if(shape.compareTo("rect")==0)
shapeInstance = new Rectangle( String2Int(coords) );
}
// Set attributes URL and ALT to Shape object
shapeAttribute.setHref(url);
shapeAttribute.setAlt(alt);
if (shapeInstance != null)
shapeInstance.setAttribute(shapeAttribute);
return shapeInstance;
}
return null;
}
/**
* String2Int converts a String-array into an array, with the same content, as integer.
* Need this function, to convert the read coordinates (after reading and splitting, they are
* saved into a string-array) to integers.
*
* @param a string array
* @return a integer array (all string represented integers become converted)
*/
private static int[] String2Int(String[] sArray)
{
int length = sArray.length;
int[] iArray = new int[length];
for (int i=0; i<sArray.length; i++)
{
iArray[i]=Integer.valueOf( sArray[i] ).intValue();
}
return iArray;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -