Skip to content

Commit

Permalink
Simplifying a bit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbylight committed Aug 6, 2023
1 parent 9c9a16b commit bbe12f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 77 deletions.
62 changes: 31 additions & 31 deletions src/main/java/org/fife/rtext/actions/CheckForUpdatesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,51 +61,51 @@ class CheckForUpdatesAction extends AppAction<RText> {
@Override
public void actionPerformed(ActionEvent e) {

RText rtext = getApplication();
Properties props = new Properties();

try {

URL url = new URL(CHECK_URL);
InputStream in = (InputStream)url.getContent();
BufferedInputStream bin = new BufferedInputStream(in);
Properties props = new Properties();
props.load(bin);
bin.close();
try (BufferedInputStream bin = new BufferedInputStream(in)) {
props.load(bin);
}

String fileVersion = props.getProperty("File.Version");
if (!"1".equals(fileVersion)) {
throw new IOException("Unsupported file version: " + fileVersion);
}
} catch (IOException ioe) {
rtext.displayException(ioe);
return;
}

RText rtext = getApplication();
String current = rtext.getVersionString();
String latest = props.getProperty("Latest.RText.Version");
String releaseDate = props.getProperty("Latest.Release.Date");
String current = rtext.getVersionString();
String latest = props.getProperty("Latest.RText.Version");
String releaseDate = props.getProperty("Latest.Release.Date");

if (current.startsWith(latest)) {
String msg = rtext.getString("UpdateStatus.UpToDate");
String title = rtext.getString("InfoDialogHeader");
JOptionPane.showMessageDialog(null, msg, title,
JOptionPane.INFORMATION_MESSAGE);
}
else {
String msg = rtext.getString("UpdateStatus.NeedToUpdate",
latest, releaseDate);
String title = rtext.getString("InfoDialogHeader");
int rc = JOptionPane.showConfirmDialog(rtext, msg, title,
JOptionPane.YES_NO_OPTION);
if (rc==JOptionPane.YES_OPTION) {
msg = rtext.getString("UpdateStatus.ShutdownReminder");
JOptionPane.showMessageDialog(rtext, msg, title,
JOptionPane.WARNING_MESSAGE);
if (!UIUtil.browse(DOWNLOAD_URL)) {
UIManager.getLookAndFeel().provideErrorFeedback(rtext);
}
if (current.startsWith(latest)) {
String msg = rtext.getString("UpdateStatus.UpToDate");
String title = rtext.getString("InfoDialogHeader");
JOptionPane.showMessageDialog(null, msg, title,
JOptionPane.INFORMATION_MESSAGE);
}
else {
String msg = rtext.getString("UpdateStatus.NeedToUpdate",
latest, releaseDate);
String title = rtext.getString("InfoDialogHeader");
int rc = JOptionPane.showConfirmDialog(rtext, msg, title,
JOptionPane.YES_NO_OPTION);
if (rc==JOptionPane.YES_OPTION) {
msg = rtext.getString("UpdateStatus.ShutdownReminder");
JOptionPane.showMessageDialog(rtext, msg, title,
JOptionPane.WARNING_MESSAGE);
if (!UIUtil.browse(DOWNLOAD_URL)) {
UIManager.getLookAndFeel().provideErrorFeedback(rtext);
}
}

} catch (IOException ioe) {
getApplication().displayException(ioe);
}

}


Expand Down
51 changes: 5 additions & 46 deletions src/main/java/org/fife/ui/search/FindInFilesThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
import java.io.IOException;
import java.io.Reader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand All @@ -34,6 +30,7 @@
import org.fife.rtext.RText;
import org.fife.ui.GUIWorkerThread;
import org.fife.ui.OS;
import org.fife.ui.rsyntaxtextarea.FileTypeUtil;
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
Expand Down Expand Up @@ -63,8 +60,6 @@ class FindInFilesThread extends GUIWorkerThread<Object> {
protected String newFilesToExamineString;
protected String occurrencesString;

//private static final Pattern TAB_PATTERN = Pattern.compile("\\t");


/**
* Constructor.
Expand Down Expand Up @@ -447,7 +442,7 @@ protected static List<File> getFilesFromDirectory(File dir) {
File[] moreFiles = dir.listFiles();
if (moreFiles==null) {
// Should never happen (as dirs return empty arrays).
return new ArrayList<>(0);
return Collections.emptyList();
}
return Arrays.asList(moreFiles);
}
Expand All @@ -462,24 +457,13 @@ protected Pattern[] getFilterStrings() {
if (tokens==null || tokens.length==0) {
return null;
}
int tokenCount = tokens.length;
Pattern[] filterStrings = new Pattern[tokenCount];
int flags = 0;
if (!OS.get().isCaseSensitive()) {
flags = Pattern.CASE_INSENSITIVE;
}
try {
for (int i=0; i<tokenCount; i++) {
String pattern = getRegexForFileFilter(tokens[i]);
filterStrings[i] = Pattern.compile(pattern, flags);
}
return Arrays.stream(tokens).map(
FileTypeUtil::fileFilterToPattern).toArray(Pattern[]::new);
} catch (PatternSyntaxException e) {
e.printStackTrace(); // Never happens.
return null;
}

return filterStrings;

}


Expand Down Expand Up @@ -517,31 +501,6 @@ private static String getHtml(Token t, RSyntaxTextArea textArea) {
}


/**
* Converts a <code>String</code> representing a wildcard file filter into
* another <code>String</code> containing a regular expression good for
* finding files that match the wildcard expressions.<br><br>
* Example: For<br><br>
* <code>String regEx = getRegexForFileFilter("*.c");</code>
* <br><br>
* <code>regEx</code> will contain <code>^.*\.c$</code>.
*
* @param filter The file filter for which to create equivalent
* regular expressions. This filter can currently only contain
* the wildcards '*' and '?'.
* @return A <code>String</code> representing an equivalent regular
* expression for the string passed in. If an error occurs,
* <code>null</code> is returned.
*/
protected static String getRegexForFileFilter(String filter) {
filter = filter.replaceAll("\\.", "\\\\."); // '.' => '\.'
filter = filter.replaceAll("\\*", ".*"); // '*' => '.*'
filter = filter.replaceAll("\\?", "."); // '?' => '.'
filter = filter.replaceAll("\\$", "\\\\\\$"); // '$' => '\$'
return "^" + filter + "$";
}


/**
* Returns whether the specified file is "filtered out" and should
* not be searched.
Expand Down

0 comments on commit bbe12f1

Please sign in to comment.