Author: scabanovich
Date: 2011-12-01 20:34:15 -0500 (Thu, 01 Dec 2011)
New Revision: 36858
Modified:
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/ImageSelectionDialog.java
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/tabs/BaseTabControl.java
Log:
JBIDE-10229
https://issues.jboss.org/browse/JBIDE-10229
GC is disposed correctly and initial selection is set.
Modified:
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/ImageSelectionDialog.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/ImageSelectionDialog.java 2011-12-02
01:05:44 UTC (rev 36857)
+++
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/ImageSelectionDialog.java 2011-12-02
01:34:15 UTC (rev 36858)
@@ -596,83 +596,84 @@
resolution.setVisible(false);
emptyColor = canvas.getForeground();
-
+ List<?> list = getInitialElementSelections();
+ if(!list.isEmpty()) {
+ Object o = list.iterator().next();
+ if(o instanceof IFile) {
+ file = (IFile)o;
+ }
+ }
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc = new GC(canvas);
- gc.setForeground(emptyColor);
- gc.fillRectangle(1, 1, canvas.getSize().x - 2, canvas.getSize().y -
2);
- // resolution.setText("");
- resolution.setVisible(false);
+ try {
+ gc.setForeground(emptyColor);
+ gc.fillRectangle(1, 1, canvas.getSize().x - 2, canvas.getSize().y -
2);
+ //resolution.setText("");
+ resolution.setVisible(false);
- if (file != null) {
- Cursor parentCursor = getShell().getCursor();
- final Cursor waitCursor = new Cursor(getShell().getDisplay(),
SWT.CURSOR_WAIT);
- Point previewPoint = new Point(0, 0);
- Point labelPoint = canvas.getSize();
- InputStream stream = null;
+ if (file != null) {
+ Cursor parentCursor = getShell().getCursor();
+ final Cursor waitCursor = new Cursor(getShell().getDisplay(),
SWT.CURSOR_WAIT);
+ Point previewPoint = new Point(0, 0);
+ Point labelPoint = canvas.getSize();
+ InputStream stream = null;
- try {
- getShell().setCursor(waitCursor);
- stream = new
FileInputStream(file.getLocation().toOSString());
+ try {
+ getShell().setCursor(waitCursor);
+ stream = new FileInputStream(file.getLocation().toOSString());
- ImageData imageData = new ImageData(stream);
- stream.close();
+ ImageData imageData = new ImageData(stream);
+ stream.close();
- if (imageData != null) {
- Image image = new Image(getShell().getDisplay(),
imageData);
+ if (imageData != null) {
+ Image image = new Image(getShell().getDisplay(), imageData);
- // set image in center
- Point imagePoint = new Point(image.getBounds().width,
+ // set image in center
+ Point imagePoint = new Point(image.getBounds().width,
image.getBounds().height);
- String imageInfo = imagePoint.x + " x " +
imagePoint.y + " px"; //$NON-NLS-1$ //$NON-NLS-2$
+ String imageInfo = imagePoint.x + " x " + imagePoint.y
+ " px"; //$NON-NLS-1$ //$NON-NLS-2$
- // change resolution if image anymore image label
- if ((imagePoint.x > labelPoint.x) || (imagePoint.y
> labelPoint.y)) {
- float ratioImage = (float) imagePoint.x / (float)
imagePoint.y;
+ // change resolution if image anymore image label
+ if ((imagePoint.x > labelPoint.x) || (imagePoint.y >
labelPoint.y)) {
+ float ratioImage = (float) imagePoint.x / (float) imagePoint.y;
- if (((imagePoint.y > labelPoint.y) &&
+ if (((imagePoint.y > labelPoint.y) &&
((labelPoint.y * ratioImage) >
labelPoint.x)) ||
((imagePoint.x > labelPoint.x) &&
((labelPoint.x / ratioImage) <
labelPoint.y))) {
- imageData = imageData.scaledTo(labelPoint.x -
10,
+ imageData = imageData.scaledTo(labelPoint.x - 10,
(int) (labelPoint.x / ratioImage));
- } else {
- imageData = imageData.scaledTo((int)
(labelPoint.y * ratioImage) -
+ } else {
+ imageData = imageData.scaledTo((int) (labelPoint.y *
ratioImage) -
10, labelPoint.y);
- }
+ }
- image.dispose();
- image = new Image(getShell().getDisplay(),
imageData);
- imagePoint.x = image.getBounds().width;
- imagePoint.y = image.getBounds().height;
- }
+ image.dispose();
+ image = new Image(getShell().getDisplay(), imageData);
+ imagePoint.x = image.getBounds().width;
+ imagePoint.y = image.getBounds().height;
+ }
- previewPoint.x = (labelPoint.x / 2) - (imagePoint.x /
2);
- previewPoint.y = (labelPoint.y / 2) - (imagePoint.y /
2);
- gc.drawImage(image, previewPoint.x, previewPoint.y);
- resolution.setVisible(true);
- resolution.setText(imageInfo);
- image.dispose();
- gc.dispose();
- }
- } catch (IOException ev) {
- //ignore
- } catch (SWTException ex) {
- //ignore (if select not image file)
- } finally {
- getShell().setCursor(parentCursor);
-
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e1) {
- // ignore
- }
- }
- }
- }
+ previewPoint.x = (labelPoint.x / 2) - (imagePoint.x / 2);
+ previewPoint.y = (labelPoint.y / 2) - (imagePoint.y / 2);
+ gc.drawImage(image, previewPoint.x, previewPoint.y);
+ resolution.setVisible(true);
+ resolution.setText(imageInfo);
+ image.dispose();
+ }
+ } catch (IOException ev) {
+ //ignore
+ } catch (SWTException ex) {
+ //ignore (if select not image file)
+ } finally {
+ getShell().setCursor(parentCursor);
+ }
+ }
+ } finally {
+ gc.dispose();
+ }
}
});
Modified:
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/tabs/BaseTabControl.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/tabs/BaseTabControl.java 2011-12-02
01:05:44 UTC (rev 36857)
+++
trunk/jst/plugins/org.jboss.tools.jst.css/src/org/jboss/tools/jst/css/dialog/tabs/BaseTabControl.java 2011-12-02
01:34:15 UTC (rev 36858)
@@ -10,7 +10,6 @@
******************************************************************************/
package org.jboss.tools.jst.css.dialog.tabs;
-import java.io.IOException;
import java.util.List;
import java.util.Map;
@@ -19,6 +18,7 @@
import org.eclipse.core.databinding.observable.Observables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
@@ -43,11 +43,11 @@
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
-import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
+import org.jboss.tools.common.web.WebUtils;
import org.jboss.tools.jst.css.CSSPlugin;
import org.jboss.tools.jst.css.dialog.FontFamilyDialog;
import org.jboss.tools.jst.css.dialog.ImageSelectionDialog;
@@ -262,15 +262,51 @@
dialog.setAllowMultiple(false);
dialog.setInput(project);
+ IFile pageFile = getPageFile();
+ String text = combo.getText();
+ if(pageFile != null && text != null && text.length() > 0) {
+ if(text.startsWith("url(") && text.endsWith(")")) {
+ text = text.substring(4, text.length() - 1);
+ }
+ text = text.replace('\\', '/');
+ IPath path = pageFile.getFullPath().removeLastSegments(1);
+ IFile selectedFile = null;
+ if(text.startsWith("/")) {
+ IPath[] paths = WebUtils.getWebContentPaths(pageFile.getProject());
+ for (IPath p: paths) {
+ selectedFile =
ResourcesPlugin.getWorkspace().getRoot().getFile(p.append(text.substring(1)));
+ if(!selectedFile.exists()) {
+ selectedFile = null;
+ } else {
+ break;
+ }
+ }
+ }
+ if(selectedFile == null) {
+ while(true) {
+ if(path.segmentCount() < 2) break;
+ if(text.startsWith("/")) {
+ text = text.substring(1);
+ } else if(text.startsWith("./")) {
+ text = text.substring(2);
+ } else if(text.startsWith("../")) {
+ text = text.substring(3);
+ path = path.removeLastSegments(1);
+ } else {
+ break;
+ }
+ }
+ path = path.append(text);
+ selectedFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+ }
+ if(selectedFile.exists()) {
+ dialog.setInitialSelection(selectedFile);
+ }
+ }
+
if (dialog.open() == ImageSelectionDialog.OK) {
- IFile imageFile = (IFile) dialog.getFirstResult();
- IWorkbenchPage page = CSSPlugin.getDefault().getWorkbench()
- .getActiveWorkbenchWindow().getActivePage();
- if (page != null
- && page.getActiveEditor() != null
- && page.getActiveEditor().getEditorInput() instanceof IFileEditorInput )
{
- IEditorInput input = page.getActiveEditor().getEditorInput();
- IFile pageFile = ((IFileEditorInput) input).getFile();
+ if(pageFile != null) {
+ IFile imageFile = (IFile) dialog.getFirstResult();
String relativePath = BaseTabControl.computeRelativePath(pageFile, imageFile);
combo.add(relativePath);
combo.setText(relativePath);
@@ -298,6 +334,17 @@
return wrapper;
}
+ private IFile getPageFile() {
+ IWorkbenchPage page = CSSPlugin.getDefault().getWorkbench()
+ .getActiveWorkbenchWindow().getActivePage();
+ if (page != null
+ && page.getActiveEditor() != null
+ && page.getActiveEditor().getEditorInput() instanceof IFileEditorInput ) {
+ return ((IFileEditorInput) page.getActiveEditor().getEditorInput()).getFile();
+ }
+ return null;
+ }
+
/**
* Build a relative path to the given base path.
*