|
|
import java.io.StringReader; | import java.io.StringReader; |
import java.text.MessageFormat; | import java.text.MessageFormat; |
| |
|
import org.eclipse.compare.PatchReader; |
import org.eclipse.compare.internal.ICompareContextIds; | import org.eclipse.compare.internal.ICompareContextIds; |
import org.eclipse.compare.internal.Utilities; | import org.eclipse.compare.internal.Utilities; |
import org.eclipse.core.resources.IFile; | import org.eclipse.core.resources.IFile; |
|
|
private final static String STORE_PATCH_FILES_ID= PAGE_NAME+".PATCH_FILES"; //$NON-NLS-1$ | private final static String STORE_PATCH_FILES_ID= PAGE_NAME+".PATCH_FILES"; //$NON-NLS-1$ |
private final static String STORE_INPUT_METHOD_ID= PAGE_NAME+".INPUT_METHOD"; //$NON-NLS-1$ | private final static String STORE_INPUT_METHOD_ID= PAGE_NAME+".INPUT_METHOD"; //$NON-NLS-1$ |
private final static String STORE_WORKSPACE_PATH_ID= PAGE_NAME+".WORKSPACE_PATH"; //$NON-NLS-1$ | private final static String STORE_WORKSPACE_PATH_ID= PAGE_NAME+".WORKSPACE_PATH"; //$NON-NLS-1$ |
//patch input constants |
|
protected final static int CLIPBOARD= 1; |
|
protected final static int FILE= 2; |
|
protected final static int WORKSPACE= 3; |
|
|
|
static final char SEPARATOR= System.getProperty("file.separator").charAt(0); //$NON-NLS-1$ | static final char SEPARATOR= System.getProperty("file.separator").charAt(0); //$NON-NLS-1$ |
| |
private boolean fShowError= false; | private boolean fShowError= false; |
|
|
//Allow error messages if the selected input actually has something selected in it | //Allow error messages if the selected input actually has something selected in it |
fShowError=true; | fShowError=true; |
switch(getInputMethod()){ | switch(getInputMethod()){ |
case FILE: |
case PatchReader.FILE: |
fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$ | fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$ |
break; | break; |
| |
case WORKSPACE: |
case PatchReader.WORKSPACE: |
fShowError = (!fTreeViewer.getSelection().isEmpty()); | fShowError = (!fTreeViewer.getSelection().isEmpty()); |
break; | break; |
| |
|
|
} | } |
| |
/* package */ String getPatchName() { | /* package */ String getPatchName() { |
if (getInputMethod()==CLIPBOARD) |
if (getInputMethod()==PatchReader.CLIPBOARD) |
return PatchMessages.InputPatchPage_Clipboard; | return PatchMessages.InputPatchPage_Clipboard; |
return getPatchFilePath(); | return getPatchFilePath(); |
} | } |
|
|
| |
String source= ""; //$NON-NLS-1$ | String source= ""; //$NON-NLS-1$ |
| |
// Create a reader for the input |
|
Reader reader= null; |
|
try { | try { |
int inputMethod= getInputMethod(); | int inputMethod= getInputMethod(); |
if (inputMethod==CLIPBOARD) { |
if (inputMethod==PatchReader.CLIPBOARD) { |
Control c= getControl(); | Control c= getControl(); |
if (c != null) { | if (c != null) { |
Clipboard clipboard= new Clipboard(c.getDisplay()); | Clipboard clipboard= new Clipboard(c.getDisplay()); |
Object o= clipboard.getContents(TextTransfer.getInstance()); |
PatchReader.readPatchFromClipboard(clipboard, patcher); |
clipboard.dispose(); |
|
if (o instanceof String) |
|
reader= new StringReader((String)o); |
|
} | } |
source= PatchMessages.InputPatchPage_Clipboard_title; | source= PatchMessages.InputPatchPage_Clipboard_title; |
} else if (inputMethod==FILE) { |
} else if (inputMethod==PatchReader.FILE) { |
String patchFilePath= getPatchFilePath(); | String patchFilePath= getPatchFilePath(); |
if (patchFilePath != null) { | if (patchFilePath != null) { |
try { | try { |
reader= new FileReader(patchFilePath); |
PatchReader.readPatchFromFile(patchFilePath, patcher); |
} catch (FileNotFoundException ex) { | } catch (FileNotFoundException ex) { |
MessageDialog.openError(null, | MessageDialog.openError(null, |
PatchMessages.InputPatchPage_PatchErrorDialog_title, | PatchMessages.InputPatchPage_PatchErrorDialog_title, |
|
|
} | } |
} | } |
source= PatchMessages.InputPatchPage_PatchFile_title; | source= PatchMessages.InputPatchPage_PatchFile_title; |
} else if (inputMethod==WORKSPACE) { |
} else if (inputMethod==PatchReader.WORKSPACE) { |
//Get the selected patch file (tree will only allow for one selection) | //Get the selected patch file (tree will only allow for one selection) |
IResource[] resources= Utilities.getResources(fTreeViewer.getSelection()); | IResource[] resources= Utilities.getResources(fTreeViewer.getSelection()); |
IResource patchFile= resources[0]; | IResource patchFile= resources[0]; |
if (patchFile!=null) { | if (patchFile!=null) { |
try { | try { |
reader= new FileReader(patchFile.getRawLocation().toFile()); |
PatchReader.readPatchFromFile(patchFile.getRawLocation(), patcher); |
} catch (FileNotFoundException ex) { | } catch (FileNotFoundException ex) { |
MessageDialog.openError(null, PatchMessages.InputPatchPage_PatchErrorDialog_title, PatchMessages.InputPatchPage_PatchFileNotFound_message); | MessageDialog.openError(null, PatchMessages.InputPatchPage_PatchErrorDialog_title, PatchMessages.InputPatchPage_PatchFileNotFound_message); |
} catch (NullPointerException nex) { | } catch (NullPointerException nex) { |
|
|
} | } |
source= PatchMessages.InputPatchPage_WorkspacePatch_title; | source= PatchMessages.InputPatchPage_WorkspacePatch_title; |
} | } |
|
} catch (IOException e) { |
// parse the input |
MessageDialog.openError(null, |
if (reader != null) { |
PatchMessages.InputPatchPage_PatchErrorDialog_title, |
try { |
PatchMessages.InputPatchPage_ParseError_message); |
patcher.parse(new BufferedReader(reader)); |
|
} catch (IOException ex) { |
|
MessageDialog.openError(null, |
|
PatchMessages.InputPatchPage_PatchErrorDialog_title, |
|
PatchMessages.InputPatchPage_ParseError_message); |
|
} |
|
} |
|
} finally { |
|
if (reader != null) { |
|
try { |
|
reader.close(); |
|
} catch (IOException x) { |
|
// silently ignored |
|
} |
|
} |
|
} | } |
| |
Diff[] diffs= patcher.getDiffs(); | Diff[] diffs= patcher.getDiffs(); |
|
|
clearErrorMessage(); | clearErrorMessage(); |
fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$ | fShowError = (fPatchFileNameField.getText() != ""); //$NON-NLS-1$ |
int state= getInputMethod(); | int state= getInputMethod(); |
setEnablePatchFile(state==FILE); |
setEnablePatchFile(state==PatchReader.FILE); |
setEnableWorkspacePatch(state==WORKSPACE); |
setEnableWorkspacePatch(state==PatchReader.WORKSPACE); |
updateWidgetEnablements(); | updateWidgetEnablements(); |
} | } |
}); | }); |
|
|
//If there is anything typed in at all | //If there is anything typed in at all |
fShowError = (!fTreeViewer.getSelection().isEmpty()); | fShowError = (!fTreeViewer.getSelection().isEmpty()); |
int state= getInputMethod(); | int state= getInputMethod(); |
setEnablePatchFile(state==FILE); |
setEnablePatchFile(state==PatchReader.FILE); |
setEnableWorkspacePatch(state==WORKSPACE); |
setEnableWorkspacePatch(state==PatchReader.WORKSPACE); |
updateWidgetEnablements(); | updateWidgetEnablements(); |
} | } |
}); | }); |
|
|
| |
boolean gotPatch= false; | boolean gotPatch= false; |
int inputMethod= getInputMethod(); | int inputMethod= getInputMethod(); |
if (inputMethod==CLIPBOARD) { |
if (inputMethod==PatchReader.CLIPBOARD) { |
Control c= getControl(); | Control c= getControl(); |
if (c != null) { | if (c != null) { |
Clipboard clipboard= new Clipboard(c.getDisplay()); | Clipboard clipboard= new Clipboard(c.getDisplay()); |
|
|
error= PatchMessages.InputPatchPage_NoTextInClipboard_message; | error= PatchMessages.InputPatchPage_NoTextInClipboard_message; |
} else | } else |
error= PatchMessages.InputPatchPage_CouldNotReadClipboard_message; | error= PatchMessages.InputPatchPage_CouldNotReadClipboard_message; |
} else if (inputMethod==FILE) { |
} else if (inputMethod==PatchReader.FILE) { |
String path= fPatchFileNameField.getText(); | String path= fPatchFileNameField.getText(); |
if (path != null && path.length() > 0) { | if (path != null && path.length() > 0) { |
File file= new File(path); | File file= new File(path); |
|
|
} else { | } else { |
error= PatchMessages.InputPatchPage_NoFileName_message; | error= PatchMessages.InputPatchPage_NoFileName_message; |
} | } |
} else if (inputMethod==WORKSPACE) { |
} else if (inputMethod==PatchReader.WORKSPACE) { |
//Get the selected patch file (tree will only allow for one selection) | //Get the selected patch file (tree will only allow for one selection) |
IResource[] resources= Utilities.getResources(fTreeViewer.getSelection()); | IResource[] resources= Utilities.getResources(fTreeViewer.getSelection()); |
if (resources!=null&&resources.length>0) { | if (resources!=null&&resources.length>0) { |
|
|
*/ | */ |
private void restoreWidgetValues() { | private void restoreWidgetValues() { |
| |
int inputMethod= FILE; |
int inputMethod= PatchReader.FILE; |
| |
IDialogSettings settings= getDialogSettings(); | IDialogSettings settings= getDialogSettings(); |
if (settings != null) { | if (settings != null) { |
|
|
//if there is a valid patch on the clipboard. This will be done in adjustToCurrentTarget() | //if there is a valid patch on the clipboard. This will be done in adjustToCurrentTarget() |
//so just set it to FILE now and, if there exists a patch on the clipboard, then clipboard | //so just set it to FILE now and, if there exists a patch on the clipboard, then clipboard |
//will be selected automatically | //will be selected automatically |
if (inputMethod == CLIPBOARD){ |
if (inputMethod == PatchReader.CLIPBOARD){ |
inputMethod=FILE; |
inputMethod=PatchReader.FILE; |
fPatchFileNameField.deselectAll(); | fPatchFileNameField.deselectAll(); |
} | } |
| |
|
|
//check to see if the current input is set to workspace - if it is switch it | //check to see if the current input is set to workspace - if it is switch it |
//back to clipboard since there is no corresponding element to go along with | //back to clipboard since there is no corresponding element to go along with |
//the tree viewer | //the tree viewer |
if (inputMethod == WORKSPACE) |
if (inputMethod == PatchReader.WORKSPACE) |
inputMethod=FILE; |
inputMethod=PatchReader.FILE; |
} | } |
} | } |
| |
|
|
| |
if (isPatchFile(reader)) { | if (isPatchFile(reader)) { |
//Set choice to workspace | //Set choice to workspace |
setInputButtonState(WORKSPACE); |
setInputButtonState(PatchReader.WORKSPACE); |
if (fTreeViewer != null && patchTarget.exists()) { | if (fTreeViewer != null && patchTarget.exists()) { |
fTreeViewer.expandToLevel(patchTarget, 0); | fTreeViewer.expandToLevel(patchTarget, 0); |
fTreeViewer.setSelection(new StructuredSelection(patchTarget)); | fTreeViewer.setSelection(new StructuredSelection(patchTarget)); |
|
|
if (o instanceof String) { | if (o instanceof String) { |
reader = new StringReader((String) o); | reader = new StringReader((String) o); |
if (isPatchFile(reader)) { | if (isPatchFile(reader)) { |
setInputButtonState(CLIPBOARD); |
setInputButtonState(PatchReader.CLIPBOARD); |
return true; | return true; |
} | } |
} | } |
|
|
private void setInputButtonState(int state) { | private void setInputButtonState(int state) { |
| |
switch (state) { | switch (state) { |
case CLIPBOARD : |
case PatchReader.CLIPBOARD : |
fUseClipboardButton.setSelection(true); | fUseClipboardButton.setSelection(true); |
fUsePatchFileButton.setSelection(false); | fUsePatchFileButton.setSelection(false); |
fUseWorkspaceButton.setSelection(false); | fUseWorkspaceButton.setSelection(false); |
break; | break; |
| |
case FILE : |
case PatchReader.FILE : |
fUseClipboardButton.setSelection(false); | fUseClipboardButton.setSelection(false); |
fUsePatchFileButton.setSelection(true); | fUsePatchFileButton.setSelection(true); |
fUseWorkspaceButton.setSelection(false); | fUseWorkspaceButton.setSelection(false); |
break; | break; |
| |
case WORKSPACE : |
case PatchReader.WORKSPACE : |
fUseClipboardButton.setSelection(false); | fUseClipboardButton.setSelection(false); |
fUsePatchFileButton.setSelection(false); | fUsePatchFileButton.setSelection(false); |
fUseWorkspaceButton.setSelection(true); | fUseWorkspaceButton.setSelection(true); |
break; | break; |
} | } |
| |
setEnablePatchFile(state==FILE); |
setEnablePatchFile(state==PatchReader.FILE); |
setEnableWorkspacePatch(state==WORKSPACE); |
setEnableWorkspacePatch(state==PatchReader.WORKSPACE); |
} | } |
| |
protected int getInputMethod() { | protected int getInputMethod() { |
if (fUseClipboardButton.getSelection()) | if (fUseClipboardButton.getSelection()) |
return CLIPBOARD; |
return PatchReader.CLIPBOARD; |
else if (fUsePatchFileButton.getSelection()) | else if (fUsePatchFileButton.getSelection()) |
return FILE; |
return PatchReader.FILE; |
else | else |
return WORKSPACE; |
return PatchReader.WORKSPACE; |
} | } |
| |
private String getPatchFilePath() { | private String getPatchFilePath() { |