use of org.eclipse.swt.graphics.Image in project cogtool by cogtool.
the class WebCrawlImportDialog method makeToolBarButton.
public ToolBar makeToolBarButton(Composite comp, SelectionListener listener, questionImages question) {
ToolBar toolBar = new ToolBar(comp, SWT.FLAT);
ToolItem item = new ToolItem(toolBar, SWT.NONE);
Image image = null;
if (question == questionImages.PLUS_IMAGE) {
image = OSUtils.MACOSX ? imagePlusMac : imagePlus;
} else if (question == questionImages.MINUS_IMAGE) {
image = OSUtils.MACOSX ? imageMinusMac : imageMinus;
} else if (question == questionImages.QUESTION_IMAGE) {
image = OSUtils.MACOSX ? questionImageMac : questionImage;
} else if (question == questionImages.QUESTION_SHADOW) {
image = OSUtils.MACOSX ? questionImageMacShadow : questionImage;
}
item.setImage(image);
item.addSelectionListener(listener);
return toolBar;
}
use of org.eclipse.swt.graphics.Image in project cogtool by cogtool.
the class FrameUIModel method dispose.
/**
* Disposes of the frameUIModel
* Disposes of the image background if any
* Disposes of each Graphical widget
* Disposes of all handlers used in the frame and the design.
*
*/
public void dispose() {
// Clear the background image
// This should be handled by the AUIModel.imageCache
Image img = backgroundImage.getImage();
if (img != null) {
// In case the ImageFigure needs to recover any internal handles
backgroundImage.setImage(null);
img.dispose();
}
// Clear the graphical widgets
Iterator<GraphicalWidget<?>> gws = figureList.values().iterator();
while (gws.hasNext()) {
GraphicalWidget<?> gw = gws.next();
gw.dispose();
}
// clears the contents
contents.dispose();
// remove any handlers used.
frame.removeAllHandlers(this);
frame.getDesign().removeAllHandlers(this);
isDisposed = true;
}
use of org.eclipse.swt.graphics.Image in project cogtool by cogtool.
the class ImportCogToolXML method parseFrame.
/**
* Imports a frame
* @param node
*/
private Frame parseFrame(Design design, Node node) throws IOException {
NodeList children = node.getChildNodes();
if (children != null) {
String frameName = getAttributeValue(node, NAME_ATTR);
if ((frameName == null) || frameName.equals("")) {
failedObjectErrors.add("Cannot create a frame with an empty name.");
return null;
}
// This adds the created frame to the design
Frame frame = getFrame(design, frameName);
addAttributes(frame, node);
Frame.setFrameDevices(frame, design.getDeviceTypes());
TransitionSource keyboardDevice = frame.getInputDevice(DeviceType.Keyboard);
TransitionSource voiceDevice = frame.getInputDevice(DeviceType.Voice);
// Some widgets have parents; so as not to require that
// all widgets of a frame occur in a particular order, we must
// resolve the parent names after all widgets have been parsed.
// Maps the child widget to the name of its parent
Map<ChildWidget, String> pendingParentSets = new LinkedHashMap<ChildWidget, String>();
// Some attributes refer to widget names; must resolve these
// after all widgets have been created.
// Currently, the only such attribute that applies to widgets
// is WidgetAttributes.SELECTION_ATTR
// Maps the attributed object to the widget name that is
// the value of the WidgetAttributes.SELECTION_ATTR attribute
Map<IAttributed, String> pendingAttrSets = new HashMap<IAttributed, String>();
// Some element groups may be referenced as members of other
// groups before being defined; this map will hold them
Map<String, FrameElementGroup> pendingGrps = new HashMap<String, FrameElementGroup>();
// Some remote labels may not be defined before they're referenced
// so keep track of those cases. Maps the owner object to
// the name of the remote label
Map<FrameElement, String> pendingRemoteLabels = new HashMap<FrameElement, String>();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
String nodeName = child.getNodeName();
if (nodeName.equalsIgnoreCase(BKG_IMAGE_PATH_ELT)) {
String backgroundImagePath = getElementText(child);
byte[] image = loadImage(backgroundImagePath);
if (image != null) {
frameLoader.set(frame, Frame.backgroundVAR, image);
frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, backgroundImagePath);
}
} else if (nodeName.equalsIgnoreCase(BKG_IMAGE_DATA_ELT)) {
String backgroundImageData = getElementText(child);
String imageName = getAttributeValue(child, NAME_ATTR);
byte[] image = null;
if (backgroundImageData != "") {
image = Base64.decode(backgroundImageData);
if ((imageName != null) && !imageName.equals("")) {
cachedImages.put(imageName, image);
}
} else if ((imageName != null) && !imageName.equals("")) {
// If imageName specified but there is no data, trust and
// try to find the last image data associated with that
// name in the cache.
image = cachedImages.get(imageName);
}
if (image != null) {
frameLoader.set(frame, Frame.backgroundVAR, image);
if ((imageName != null) && !imageName.equals("")) {
frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageName);
}
}
} else if (nodeName.equalsIgnoreCase(ORIGIN_ELT)) {
double x = Double.parseDouble(getAttributeValue(child, X_ATTR));
double y = Double.parseDouble(getAttributeValue(child, Y_ATTR));
DoublePoint origin = new DoublePoint(x, y);
frameLoader.set(frame, Frame.originVAR, origin);
} else if (nodeName.equalsIgnoreCase(SPEAKER_TEXT_ELT)) {
frameLoader.set(frame, Frame.speakerTextVAR, getElementText(child));
} else if (nodeName.equalsIgnoreCase(LISTEN_TIME_SECS_ELT)) {
frameLoader.set(frame, Frame.listenTimeVAR, Double.parseDouble(getElementText(child)));
} else if (nodeName.equalsIgnoreCase(WIDGET_ELT)) {
IWidget w = parseWidget(design, frame, pendingParentSets, pendingAttrSets, pendingRemoteLabels, child);
if (w != null) {
frame.addWidget(w);
} else {
w = new Widget(null, WidgetType.Noninteractive);
Image wImage = GraphicsUtil.getImageFromResource("edu/cmu/cs/hcii/cogtool/resources/warning.jpg");
//w.setImage(wImage.getBytes());
frame.addWidget(w);
}
} else if (nodeName.equalsIgnoreCase(ELTGROUP_ELT)) {
FrameElementGroup g = parseEltGroup(design, frame, pendingGrps, child);
if (g != null) {
String eltGrpName = g.getName();
pendingGrps.remove(eltGrpName);
eltGrpName = NamedObjectUtil.makeNameUnique(eltGrpName, frame.getEltGroups());
g.setName(eltGrpName);
frame.addEltGroup(g);
}
} else if (nodeName.equalsIgnoreCase(KEYBOARD_TRANSITIONS_ELT)) {
if (keyboardDevice != null) {
parseTransitions(design, keyboardDevice, child);
} else {
failedObjectErrors.add("Keyboard transitions require that Design have a Keyboard device");
}
} else if (nodeName.equalsIgnoreCase(VOICE_TRANSITIONS_ELT)) {
if (voiceDevice != null) {
parseTransitions(design, voiceDevice, child);
} else {
failedObjectErrors.add("Voice transitions require that Design have a Voice device");
}
}
}
if (frame.getName() != null) {
// Handle any forward references for remote labels
Iterator<Map.Entry<FrameElement, String>> labelRefs = pendingRemoteLabels.entrySet().iterator();
while (labelRefs.hasNext()) {
Map.Entry<FrameElement, String> labelRef = labelRefs.next();
setRemoteLabel(frame, labelRef.getValue(), labelRef.getKey(), null);
}
// If any "pending" element groups still exist, then there
// is an error -- an element group that didn't exist!
Iterator<FrameElementGroup> missingGrps = pendingGrps.values().iterator();
StringBuilder errorMsg = new StringBuilder();
while (missingGrps.hasNext()) {
FrameElementGroup missingGrp = missingGrps.next();
errorMsg.append("Missing widget or group, named: ");
errorMsg.append(missingGrp.getName());
errorMsg.append(" as member of the following groups: ");
Iterator<FrameElementGroup> inGrps = missingGrp.getEltGroups().iterator();
String separator = "";
while (inGrps.hasNext()) {
errorMsg.append(separator + inGrps.next().getName());
separator = ", ";
}
failedObjectErrors.add(errorMsg.toString());
errorMsg.delete(0, errorMsg.length());
}
Iterator<Map.Entry<ChildWidget, String>> childToParentSet = pendingParentSets.entrySet().iterator();
// relationships
while (childToParentSet.hasNext()) {
Map.Entry<ChildWidget, String> childToParent = childToParentSet.next();
String parentName = childToParent.getValue();
if (!"".equals(parentName)) {
ChildWidget child = childToParent.getKey();
AParentWidget parent = (AParentWidget) frame.getWidget(parentName);
parent.addItem(child);
child.setParent(parent);
}
}
Iterator<Map.Entry<IAttributed, String>> selnAttrToSet = pendingAttrSets.entrySet().iterator();
// that used widget names as values.
while (selnAttrToSet.hasNext()) {
Map.Entry<IAttributed, String> selnAttr = selnAttrToSet.next();
String widgetName = selnAttr.getValue();
IWidget attrValue = "".equals(widgetName) ? null : frame.getWidget(widgetName);
// At the moment, all occurrences that use pendingAttrSets
// are instances of PullDownHeader for
// WidgetAttributes.SELECTION_ATTR
selnAttr.getKey().setAttribute(WidgetAttributes.SELECTION_ATTR, attrValue);
}
return frame;
}
}
return null;
}
use of org.eclipse.swt.graphics.Image in project cogtool by cogtool.
the class DesignExportToHTML method exportToHTML.
public void exportToHTML(Design d, String dir, Cancelable cancelState, ProgressCallback progressState) {
// No need to duplicate here; we're already in the child thread
// and the design had to have been duplicated in the main thread!
design = d;
destDirectory = dir;
// Performed by the child thread
// Create a file object for DestDir and test if it is actually
// a directory
parentDir = new File(destDirectory);
if (!parentDir.isDirectory()) {
// If there is no directory specified, we should fail here.
throw new IllegalArgumentException("Create Web pages called " + "without a directory");
}
Set<Frame> frameSet = design.getFrames();
int frameCount = frameSet.size();
// Start at 0 leaving one extra "count" for overlib copy
// Use "double" to force proper division when setting progress below
double progressCount = 0.0;
// call buildFrameList to build the lookup maps.
buildFrameList();
Iterator<Frame> iter = frameSet.iterator();
ImageLoader imageLoader = new ImageLoader();
String html = null;
Image img = null;
//Note: Very long while loop in terms of code
while ((!cancelState.isCanceled()) && iter.hasNext()) {
Frame frame = iter.next();
try {
//below function, "buildFrameImage", takes in a frame and returns its image
img = buildFrameImage(frame);
imageLoader.data = new ImageData[] { img.getImageData() };
String imageName = getFrameFileName(frame, ".jpg");
// create new FILE handler for the new imageSaver's use
File imageFile = new File(parentDir, imageName);
try {
imageLoader.save(imageFile.getCanonicalPath(), SWT.IMAGE_JPEG);
} catch (IOException ex) {
// We can continue even with exceptions on individual images
throw new ImageException("Failed saving image for HTML export", ex);
}
} finally {
// dispose the image, it's not needed any more.
img.dispose();
}
try {
// write HTML to destDir
FileWriter fileOut = null;
BufferedWriter writer = null;
try {
// Use the local file name, and not the complete path.
html = buildFrameHTML(frame);
File htmlFile = new File(parentDir, getFrameFileName(frame, ".html"));
fileOut = new FileWriter(htmlFile);
writer = new BufferedWriter(fileOut);
writer.write(html);
} finally {
if (writer != null) {
writer.close();
} else if (fileOut != null) {
fileOut.close();
}
}
} catch (IOException ex) {
throw new ExportIOException("Could not save HTML for export ", ex);
}
// Update the progress count
progressCount += 1.0;
progressState.updateProgress(progressCount / frameCount, SWTStringUtil.insertEllipsis(frame.getName(), 250, StringUtil.NO_FRONT, SWTStringUtil.DEFAULT_FONT));
//end of getting frames
}
//make sure user did not cancel, and then create folder "build"
if (!cancelState.isCanceled()) {
File buildDir = new File(parentDir, "build");
if (!buildDir.exists()) {
if (!buildDir.mkdir()) {
throw new ExportIOException("Could not create build directory");
}
}
try {
// Write out the index page.
FileWriter fileOut = null;
BufferedWriter writer = null;
try {
// Use the local file name, and not the complete path.
//This creates the main page, needs a little styling
html = buildIndexPage();
File htmlFile = new File(parentDir, "index.html");
fileOut = new FileWriter(htmlFile);
writer = new BufferedWriter(fileOut);
writer.write(html);
} finally {
if (writer != null) {
writer.close();
} else if (fileOut != null) {
fileOut.close();
}
}
} catch (IOException ex) {
throw new ExportIOException("Could not save index.html for export ", ex);
}
//Here we import all the resources from the standard directory
InputStream overlibStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/overlib.js");
if (overlibStream == null) {
throw new ExportIOException("Could not locate overlib.js resource");
}
File overlibFile = new File(buildDir, "overlib.js");
InputStream containerCoreStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/container_core.js");
if (containerCoreStream == null) {
throw new ExportIOException("Could not locate container_core.js resource");
}
File containerCoreFile = new File(buildDir, "container_core.js");
InputStream fontsStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/fonts-min.css");
if (fontsStream == null) {
throw new ExportIOException("Could not locate fonts-min.css resource");
}
File fontsFile = new File(buildDir, "fonts-min.css");
InputStream menuStyleStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/menu.css");
if (menuStyleStream == null) {
throw new ExportIOException("Could not locate menu.css resource");
}
File menuStyleFile = new File(buildDir, "menu.css");
InputStream menuStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/menu.js");
if (menuStream == null) {
throw new ExportIOException("Could not locate menu.js resource");
}
File menuFile = new File(buildDir, "menu.js");
//As you can see, lots of yahoo fancy shmancy
InputStream eventStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/yahoo-dom-event.js");
if (eventStream == null) {
throw new ExportIOException("Could not locate yahoo-dom-event.js resource");
}
File eventFile = new File(buildDir, "yahoo-dom-event.js");
InputStream spriteStream = ClassLoader.getSystemResourceAsStream("edu/cmu/cs/hcii/cogtool/resources/ExportToHTML/sprite.png");
if (spriteStream == null) {
throw new ExportIOException("Could not locate sprite.png resource");
}
File spriteFile = new File(buildDir, "sprite.png");
try {
FileUtil.copyStreamToFile(overlibStream, overlibFile);
FileUtil.copyStreamToFile(containerCoreStream, containerCoreFile);
FileUtil.copyStreamToFile(fontsStream, fontsFile);
FileUtil.copyStreamToFile(menuStyleStream, menuStyleFile);
FileUtil.copyStreamToFile(menuStream, menuFile);
FileUtil.copyStreamToFile(eventStream, eventFile);
FileUtil.copyStreamToFile(spriteStream, spriteFile);
} catch (IOException ex) {
throw new ExportIOException("Failed to create file", ex);
}
}
// clear the look up object
frameLookUp.clear();
frameLookUp = null;
}
use of org.eclipse.swt.graphics.Image in project cogtool by cogtool.
the class GraphicsUtil method getImageBounds.
/**
* A utility to load an image and then dispose it from a byte array.
* This utility returns the size of the image.
* This is not a good function to call often. its SLOW.
*
* If the image is null, then returns a new rectangle (0,0,0,0);
* TODO: should this return null
* @param image
* @return
*/
public static DoubleRectangle getImageBounds(byte[] image) {
if (image == null) {
return new DoubleRectangle(0, 0, 0, 0);
}
try {
Image img = new Image(null, new ByteArrayInputStream(image));
Rectangle rect = img.getBounds();
img.dispose();
return new DoubleRectangle(rect.x, rect.y, rect.width, rect.height);
} catch (SWTException ex) {
throw new ImageException("Failed to get image bounds.", ex);
}
}
Aggregations