Search in sources :

Example 1 with SWTException

use of org.eclipse.swt.SWTException in project cogtool by cogtool.

the class DictionaryEditorUI method updateView.

protected void updateView() {
    Table dictTable = view.getDictTable();
    int selectionCount = 0;
    try {
        selectionCount = dictTable.getSelectionCount();
    } catch (SWTException e) {
        return;
    }
    if (selectionCount == 1) {
        int index = dictTable.getSelectionIndex();
        DictEntry entry = dictionary.getEntry(index);
        if (entry != null) {
            String url = null;
            String space = null;
            if (entry.algorithm instanceof ISitedTermSimilarity) {
                url = ((ISitedTermSimilarity) entry.algorithm).getContextSite();
                view.setURLEnabled(true);
                view.setSpaceEnabled(false);
            } else if (entry.algorithm instanceof LSASimilarity) {
                LSASimilarity lsa = (LSASimilarity) entry.algorithm;
                space = lsa.getSpace();
                url = lsa.getURL();
                if (url == null) {
                    url = LSASimilarity.DEFAULT_LSA_URL;
                }
                view.setURLEnabled(true);
                view.setSpaceEnabled(true);
            } else if (entry.algorithm instanceof GensimLSASimilarity) {
                GensimLSASimilarity lsa = (GensimLSASimilarity) entry.algorithm;
                space = lsa.getSpace();
                url = lsa.getURL();
                if (url == null) {
                    url = GensimLSASimilarity.DEFAULT_LSA_URL;
                }
                view.setURLEnabled(true);
                view.setSpaceEnabled(true);
            } else {
                view.setURLEnabled(false);
                view.setSpaceEnabled(false);
            }
            view.setURL(url);
            view.setSpace(space);
        } else {
            TableItem row = dictTable.getItem(index);
            Combo c = (Combo) row.getData();
            int seln = c.getSelectionIndex();
            boolean isLSA = (seln == DictionaryEditorUIModel.LSA_INDEX);
            view.setURLEnabled((seln == DictionaryEditorUIModel.GOOGLE_WORD_INDEX) || (seln == DictionaryEditorUIModel.GOOGLE_PHRASE_INDEX) || isLSA);
            view.setSpaceEnabled(isLSA);
        }
    } else {
        // disable if 0 or more than 1 are selected
        view.setURL(null);
        view.setSpace(null);
        view.setURLEnabled(false);
        view.setSpaceEnabled(false);
    }
}
Also used : Table(org.eclipse.swt.widgets.Table) SWTException(org.eclipse.swt.SWTException) TableItem(org.eclipse.swt.widgets.TableItem) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) GensimLSASimilarity(edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity) LSASimilarity(edu.cmu.cs.hcii.cogtool.model.LSASimilarity) Combo(org.eclipse.swt.widgets.Combo) ISitedTermSimilarity(edu.cmu.cs.hcii.cogtool.model.ISitedTermSimilarity) Point(org.eclipse.swt.graphics.Point) GensimLSASimilarity(edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity)

Example 2 with SWTException

use of org.eclipse.swt.SWTException in project cogtool by cogtool.

the class DictionaryEditorUI method setViewEnabledState.

protected void setViewEnabledState(Boolean context) {
    try {
        Table dictTable = view.getDictTable();
        int selectionCount = dictTable.getSelectionCount();
        boolean pendingSelected = dictTable.isSelected(dictTable.getItemCount() - 1);
        boolean enabled = !pendingSelected && (selectionCount >= 1);
        setEnabled(CogToolLID.Delete, context, enabled);
        enabled = selectionCount == 1;
        setEnabled(DictionaryEditorLID.SetSimilarity, context, enabled);
        setEnabled(DictionaryEditorLID.SetGoalString, context, enabled);
        setEnabled(DictionaryEditorLID.SetSearchString, context, enabled);
    } catch (SWTException e) {
    // ignore
    }
}
Also used : Table(org.eclipse.swt.widgets.Table) SWTException(org.eclipse.swt.SWTException) Point(org.eclipse.swt.graphics.Point)

Example 3 with SWTException

use of org.eclipse.swt.SWTException 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);
    }
}
Also used : SWTException(org.eclipse.swt.SWTException) ByteArrayInputStream(java.io.ByteArrayInputStream) Rectangle(org.eclipse.swt.graphics.Rectangle) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) Image(org.eclipse.swt.graphics.Image)

Example 4 with SWTException

use of org.eclipse.swt.SWTException in project tdi-studio-se by Talend.

the class CodeGeneratorProgressMonitor method runEventLoop.

/**
     * Runs an event loop.
     */
private void runEventLoop() {
    // Only run the event loop so often, as it is expensive on some platforms
    // (namely Motif).
    final long t = System.currentTimeMillis();
    if (t - lastTime < tTHRESH) {
        return;
    }
    lastTime = t;
    // Run the event loop.
    if (CommonUIPlugin.isFullyHeadless()) {
        return;
    }
    final Display disp = DisplayUtils.getDisplay();
    if (disp == null) {
        return;
    }
    disp.syncExec(new Runnable() {

        @Override
        public void run() {
            for (; ; ) {
                try {
                    if (!disp.readAndDispatch()) {
                        break;
                    }
                } catch (SWTException se) {
                // do nothing;
                }
                // constantly generating events.
                if (System.currentTimeMillis() - t > tMAX) {
                    break;
                }
            }
        }
    });
}
Also used : SWTException(org.eclipse.swt.SWTException) Display(org.eclipse.swt.widgets.Display)

Example 5 with SWTException

use of org.eclipse.swt.SWTException in project cogtool by cogtool.

the class FrameUIModel method addFrameChangeListeners.

/**
     * Add listeners for when things change on the frame.
     */
protected void addFrameChangeListeners() {
    AlertHandler frameChangeHandler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            Frame.WidgetChange chg = (Frame.WidgetChange) alert;
            IWidget chgWidget = chg.getChangeElement();
            if (chg != null) {
                // action dictated by the change.
                switch(chg.action) {
                    // Add the graphical representation of the widget
                    case Frame.WidgetChange.ELEMENT_ADD:
                        createGraphicalWidget(chgWidget);
                        raiseAlert(new FrameUIModel.WidgetShapeImageChange(FrameUIModel.this, chgWidget));
                        break;
                    // Remove the graphical representation of the widget
                    case Frame.WidgetChange.ELEMENT_DELETE:
                        removeWidget(chgWidget);
                        raiseAlert(new FrameUIModel.WidgetShapeImageChange(FrameUIModel.this, chgWidget));
                        break;
                    // Update all existing widgets to the new color
                    case Frame.WidgetChange.WIDGET_COLORS_CHANGED:
                        Iterator<GraphicalWidget<?>> gws = figureList.values().iterator();
                        // Update graphical widgets
                        while (gws.hasNext()) {
                            GraphicalWidget<?> gw = gws.next();
                            gw.setColor(frame.getWidgetColor());
                        //                                    gw.setFastMode(false);
                        }
                        // Update potential temporary widget
                        contents.setWidgetColor(frame.getWidgetColor());
                        break;
                }
            }
            // Draw the viewable widgets.
            // Adds any new items from the lists.. or moves them
            drawWidgets();
        }
    };
    // Add the new handler for frame changes to the list of things to
    // alert to changes on the frame
    frame.addHandler(this, Frame.WidgetChange.class, frameChangeHandler);
    // A separate handler is required to take care of changes of the
    // background on the frame.
    AlertHandler frameBackgroundChangeHandler = new AlertHandler() {

        public void handleAlert(EventObject alert) {
            Frame.BackgroundImageChange chg = (Frame.BackgroundImageChange) alert;
            if (chg != null) {
                // Dispose the old background image if there was one
                if (backgroundImage.getImage() != null) {
                    backgroundImage.getImage().dispose();
                }
                byte[] bgImg = frame.getBackgroundImage();
                // If the image is null, don't create it.
                if (bgImg != null) {
                    // the cache is probably out of date
                    try {
                        Image img = new Image(null, new ByteArrayInputStream(bgImg));
                        //                                AUIModel.imageCache.put(frame, img);
                        // set the new image to the background
                        backgroundImage.setImage(img);
                        // get the size of the image
                        org.eclipse.swt.graphics.Rectangle bounds = img.getBounds();
                        // resize background image with the new bounds
                        backgroundImage.setSize(bounds.width, bounds.height);
                    } catch (SWTException ex) {
                        throw new GraphicsUtil.ImageException("Setting frame background image failed", ex);
                    }
                } else {
                    // Clear the background image.
                    backgroundImage.setImage(null);
                    backgroundImage.setSize(0, 0);
                }
            }
            drawWidgets();
            // Need to raise Alert after draw widgets since
            // the alert call seems to reset the "bounds"
            raiseAlert(new FrameUIModel.WidgetShapeImageChange(FrameUIModel.this, null));
        }
    };
    // Add this handler to the list for changes in the background image
    frame.addHandler(this, Frame.BackgroundImageChange.class, frameBackgroundChangeHandler);
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) Image(org.eclipse.swt.graphics.Image) EventObject(java.util.EventObject) SWTException(org.eclipse.swt.SWTException) ByteArrayInputStream(java.io.ByteArrayInputStream) AlertHandler(edu.cmu.cs.hcii.cogtool.util.AlertHandler) GraphicsUtil(edu.cmu.cs.hcii.cogtool.util.GraphicsUtil) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Aggregations

SWTException (org.eclipse.swt.SWTException)11 Image (org.eclipse.swt.graphics.Image)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IWidget (edu.cmu.cs.hcii.cogtool.model.IWidget)2 GraphicsUtil (edu.cmu.cs.hcii.cogtool.util.GraphicsUtil)2 Point (org.eclipse.swt.graphics.Point)2 Table (org.eclipse.swt.widgets.Table)2 StopWatch (alma.acs.util.StopWatch)1 CogToolWorkThread (edu.cmu.cs.hcii.cogtool.CogToolWorkThread)1 DoubleRectangle (edu.cmu.cs.hcii.cogtool.model.DoubleRectangle)1 DoubleSize (edu.cmu.cs.hcii.cogtool.model.DoubleSize)1 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)1 GensimLSASimilarity (edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity)1 DictEntry (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry)1 ISitedTermSimilarity (edu.cmu.cs.hcii.cogtool.model.ISitedTermSimilarity)1 LSASimilarity (edu.cmu.cs.hcii.cogtool.model.LSASimilarity)1 AlertHandler (edu.cmu.cs.hcii.cogtool.util.AlertHandler)1 RcvrImageException (edu.cmu.cs.hcii.cogtool.util.RcvrImageException)1 ThreadManager (edu.cmu.cs.hcii.cogtool.util.ThreadManager)1 ScalableFrameFigure (edu.cmu.cs.hcii.cogtool.view.ScalableFrameFigure)1