Skip to content

Commit

Permalink
Merge pull request #10 from braindevices/master
Browse files Browse the repository at this point in the history
Fixes groups and adds optional 2.9.2 save ordering
  • Loading branch information
koppor committed May 20, 2014
2 parents 97fc8bb + b6a89a8 commit 9ec813f
Show file tree
Hide file tree
Showing 12 changed files with 1,451 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ buildant/
.settings/
.classpath
.project
jabref.xml
131 changes: 131 additions & 0 deletions src/main/java/net/sf/jabref/BibtexEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public String[] getRequiredFields()
{
return _type.getRequiredFields();
}

public String[] getUserDefinedFields()
{

return Globals.prefs.getStringArray(JabRefPreferences.WRITEFIELD_USERDEFINEDORDER);
}

/**
* Returns an set containing the names of all fields that are
Expand Down Expand Up @@ -409,6 +415,131 @@ public void removePropertyChangeListener(VetoableChangeListener listener)
* isDisplayableField(String).
*/
public void write(Writer out, FieldFormatter ff, boolean write) throws IOException {
switch (Globals.prefs.getInt(JabRefPreferences.WRITEFIELD_SORTSTYLE)) {
case 0:
writeSorted(out, ff, write);
break;
case 1:
writeUnsorted(out, ff, write);
break;
case 2:
writeUserOrder(out,ff,write);
break;
}


}


/**
* user defined order
* @param out
* @param ff A formatter to filter field contents before writing
* @param write True if this is a write, false if it is a display. The write will not include non-writeable fields if it is a write, otherwise non-displayable fields will be ignored. Refer to GUIGlobals for isWriteableField(String) and isDisplayableField(String).
* @throws IOException
*/
private void writeUserOrder(Writer out, FieldFormatter ff, boolean write) throws IOException {
// Write header with type and bibtex-key.
out.write("@"+_type.getName()+"{");

String str = Util.shaveString(getField(BibtexFields.KEY_FIELD));
out.write(((str == null) ? "" : str)+","+Globals.NEWLINE);
HashMap<String, String> written = new HashMap<String, String>();
written.put(BibtexFields.KEY_FIELD, null);
boolean hasWritten = false;

// Write user defined fields first.
String[] s = getUserDefinedFields();
if (s != null) {
//do not sort, write as it is.
for (String value : s) {
if (!written.containsKey(value)) { // If field appears both in req. and opt. don't repeat.
hasWritten = hasWritten | writeField(value, out, ff, hasWritten, false);
written.put(value, null);
}
}
}

// Then write remaining fields in alphabetic order.
boolean first = true, previous = true;
previous = false;
//STA get remaining fields
TreeSet<String> remainingFields = new TreeSet<String>();
for (String key : _fields.keySet()){
//iterate through all fields
boolean writeIt = (write ? BibtexFields.isWriteableField(key) :
BibtexFields.isDisplayableField(key));
//find the ones has not been written.
if (!written.containsKey(key) && writeIt)
remainingFields.add(key);
}
//END get remaining fields

first = previous;
for (String field: remainingFields) {
hasWritten = hasWritten | writeField(field, out, ff, hasWritten, hasWritten && first);
first = false;
}

// Finally, end the entry.
out.write((hasWritten ? Globals.NEWLINE : "")+"}"+Globals.NEWLINE);

}

/**
* old style ver<=2.9.2, write fields in the order of requiredFields, optionalFields and other fields, but does not sort the fields.
* @param out
* @param ff A formatter to filter field contents before writing
* @param write True if this is a write, false if it is a display. The write will not include non-writeable fields if it is a write, otherwise non-displayable fields will be ignored. Refer to GUIGlobals for isWriteableField(String) and isDisplayableField(String).
* @throws IOException
*/
private void writeUnsorted(Writer out, FieldFormatter ff, boolean write) throws IOException {
// Write header with type and bibtex-key.
out.write("@"+_type.getName().toUpperCase(Locale.US)+"{");

String str = Util.shaveString(getField(BibtexFields.KEY_FIELD));
out.write(((str == null) ? "" : str)+","+Globals.NEWLINE);
HashMap<String, String> written = new HashMap<String, String>();
written.put(BibtexFields.KEY_FIELD, null);
boolean hasWritten = false;
// Write required fields first.
String[] s = getRequiredFields();
if (s != null) for (int i=0; i<s.length; i++) {
hasWritten = hasWritten | writeField(s[i], out, ff, hasWritten,false);
written.put(s[i], null);
}
// Then optional fields.
s = getOptionalFields();
if (s != null) for (int i=0; i<s.length; i++) {
if (!written.containsKey(s[i])) { // If field appears both in req. and opt. don't repeat.
//writeField(s[i], out, ff);
hasWritten = hasWritten | writeField(s[i], out, ff, hasWritten,false);
written.put(s[i], null);
}
}
// Then write remaining fields in alphabetic order.
TreeSet<String> remainingFields = new TreeSet<String>();
for (String key : _fields.keySet()){
boolean writeIt = (write ? BibtexFields.isWriteableField(key) :
BibtexFields.isDisplayableField(key));
if (!written.containsKey(key) && writeIt)
remainingFields.add(key);
}
for (String field: remainingFields)
hasWritten = hasWritten | writeField(field, out, ff, hasWritten,false);

// Finally, end the entry.
out.write((hasWritten ? Globals.NEWLINE : "")+"}"+Globals.NEWLINE);
}

/**
* new style ver>=2.10, sort the field for requiredFields, optionalFields and other fields separately
* @param out
* @param ff A formatter to filter field contents before writing
* @param write True if this is a write, false if it is a display. The write will not include non-writeable fields if it is a write, otherwise non-displayable fields will be ignored. Refer to GUIGlobals for isWriteableField(String) and isDisplayableField(String).
* @throws IOException
*/
private void writeSorted(Writer out, FieldFormatter ff, boolean write) throws IOException {
// Write header with type and bibtex-key.
out.write("@"+_type.getName()+"{");

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/BibtexEntryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public String[] getOptionalFields()
{
return new String[]
{
"volume", "number", "pages", "month", "note", //- "volume", "pages", "part", "eid"
"volume", "pages", "number", "month", "note", //- "volume", "pages", "part", "eid"
};
}

Expand Down
121 changes: 121 additions & 0 deletions src/main/java/net/sf/jabref/FileTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
package net.sf.jabref;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.*;
import javax.swing.event.ChangeListener;
Expand Down Expand Up @@ -44,6 +49,14 @@ public class FileTab extends JPanel implements PrefsTab {
doNotResolveStringsFor;
private JSpinner autoSaveInterval;
private boolean origAutoSaveSetting = false;

//for LWang_AdjustableFieldOrder
// private JRadioButton sortFieldInAlphabetaOrder,unSortFieldStyle,orderAsUserDefined;
private ButtonGroup bgFieldOrderStyle;
// int fieldOrderStyle;
private JTextField userDefinedFieldOrder;

private JCheckBox wrapFieldLine;

public FileTab(JabRefFrame frame, JabRefPreferences prefs) {
_prefs = prefs;
Expand All @@ -68,6 +81,18 @@ public FileTab(JabRefFrame frame, JabRefPreferences prefs) {
bg.add(resolveStringsAll);
bg.add(resolveStringsStandard);

//for LWang_AdjustableFieldOrder
// ButtonGroup bgFieldOrderStyle=new ButtonGroup();
// sortFieldInAlphabetaOrder = new JRadioButton(Globals.lang("Sort fields in alphabeta order (as ver >= 2.10)"));
// unSortFieldStyle = new JRadioButton(Globals.lang("Do not sort fields (as ver<=2.9.2)"));
// orderAsUserDefined= new JRadioButton(Globals.lang("Save fields as user defined order"));
// bgFieldOrderStyle.add(sortFieldInAlphabetaOrder);
// bgFieldOrderStyle.add(unSortFieldStyle);
// bgFieldOrderStyle.add(orderAsUserDefined);

userDefinedFieldOrder=new JTextField(_prefs.get(JabRefPreferences.WRITEFIELD_USERDEFINEDORDER)); //need to use JcomboBox in the future


// This is sort of a quick hack
newlineSeparator = new JComboBox<String>(new String[] {"CR", "CR/LF", "LF"});

Expand Down Expand Up @@ -149,12 +174,93 @@ public void stateChanged(ChangeEvent changeEvent) {
builder.append(includeEmptyFields);
builder.append(new JPanel());
builder.nextLine();


wrapFieldLine = new JCheckBox(Globals.lang("Wrap fields as ver 2.9.2"));
builder.append(wrapFieldLine);
builder.nextLine();
//for LWang_AdjustableFieldOrder
String [] _rbs0={"Sort fields in alphabeta order (as ver 2.10)", "Sort fields in old fasion (as ver 2.9.2)","Save fields as user defined order"};
ArrayList<String> _rbs = new ArrayList<String>();
for (String _rb:_rbs0){
_rbs.add(Globals.lang(_rb));
}
bgFieldOrderStyle=createRadioBg(_rbs);
userDefinedFieldOrder=new JTextField(_prefs.get(JabRefPreferences.WRITEFIELD_USERDEFINEDORDER)); //need to use JcomboBox in the future
createAdFieldOrderBg(builder, bgFieldOrderStyle, userDefinedFieldOrder);
// builder.append(sortFieldInAlphabetaOrder);
// builder.nextLine();
// builder.append(unSortFieldStyle);
// builder.nextLine();
// builder.append(orderAsUserDefined);
// builder.append(userDefinedFieldOrder);
// builder.nextLine();


JPanel pan = builder.getPanel();
pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout());
add(pan, BorderLayout.CENTER);
}

private ButtonGroup createRadioBg(Iterable<String> radioButtonLabels){
ButtonGroup _bg=new ButtonGroup();
for (String _s: radioButtonLabels){
JRadioButton _rb=new JRadioButton(_s);
_bg.add(_rb);

}
return _bg;
}

private int getBgValue(ButtonGroup bg){
int _i=0;
for (Enumeration<AbstractButton> _it = bg.getElements(); _it.hasMoreElements();){
if (_it.nextElement().isSelected()){
return _i;
}
_i++;

}
return 0;

}
private void setBgSelected(ButtonGroup bg,int x){
int _i=0;

for (Enumeration<AbstractButton> _it = bg.getElements(); _it.hasMoreElements();){
if (_i==x){
_it.nextElement().setSelected(true);

} else {
_it.nextElement().setSelected(false);

}
_i++;

}

}


// private void setValueFieldOrderStyle(){
// fieldOrderStyle=getBgValue(bgFieldOrderStyle);
// }

private void createAdFieldOrderBg(DefaultFormBuilder builder, ButtonGroup bg, JTextField jtf){
//for LWang_AdjustableFieldOrder



for (Enumeration<AbstractButton> _it = bg.getElements(); _it.hasMoreElements();){
builder.append(_it.nextElement());
builder.nextLine();
}
builder.append(jtf);
builder.nextLine();
}



public void setValues() {
openLast.setSelected(_prefs.getBoolean("openLastEdited"));
Expand All @@ -171,6 +277,7 @@ public void setValues() {
}

//preserveFormatting.setSelected(_prefs.getBoolean("preserveFieldFormatting"));
wrapFieldLine.setSelected(_prefs.getBoolean(JabRefPreferences.WRITEFIELD_WRAPFIELD));
autoDoubleBraces.setSelected(_prefs.getBoolean("autoDoubleBraces"));
resolveStringsAll.setSelected(_prefs.getBoolean("resolveStringsAllFields"));
resolveStringsStandard.setSelected(!resolveStringsAll.isSelected());
Expand All @@ -186,6 +293,11 @@ public void setValues() {
includeEmptyFields.setSelected(_prefs.getBoolean("includeEmptyFields"));
camelCase.setSelected(_prefs.getBoolean(JabRefPreferences.WRITEFIELD_CAMELCASENAME));
sameColumn.setSelected(_prefs.getBoolean(JabRefPreferences.WRITEFIELD_ADDSPACES));

//for LWang_AdjustableFieldOrder
setBgSelected(bgFieldOrderStyle, _prefs.getInt(JabRefPreferences.WRITEFIELD_SORTSTYLE));
userDefinedFieldOrder.setText(_prefs.get(JabRefPreferences.WRITEFIELD_USERDEFINEDORDER));

}

public void storeSettings() {
Expand Down Expand Up @@ -218,6 +330,13 @@ public void storeSettings() {
_prefs.putBoolean(JabRefPreferences.WRITEFIELD_CAMELCASENAME, camelCase.isSelected());
_prefs.putBoolean(JabRefPreferences.WRITEFIELD_ADDSPACES, sameColumn.isSelected());
doNotResolveStringsFor.setText(_prefs.get("doNotResolveStringsFor"));

//for LWang_AdjustableFieldOrder
_prefs.putInt(JabRefPreferences.WRITEFIELD_SORTSTYLE,getBgValue(bgFieldOrderStyle));
_prefs.put(JabRefPreferences.WRITEFIELD_USERDEFINEDORDER,userDefinedFieldOrder.getText().trim());
_prefs.putBoolean(JabRefPreferences.WRITEFIELD_WRAPFIELD,wrapFieldLine.isSelected());


boolean updateSpecialFields = false;
if (!bracesAroundCapitalsFields.getText().trim().equals(_prefs.get("putBracesAroundCapitals"))) {
_prefs.put("putBracesAroundCapitals", bracesAroundCapitalsFields.getText());
Expand Down Expand Up @@ -248,4 +367,6 @@ public boolean readyToClose() {
public String getTabName() {
return Globals.lang("File");
}


}
12 changes: 11 additions & 1 deletion src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ public class JabRefPreferences {
EXPORT_SECONDARY_SORT_FIELD = "exportSecSort",
EXPORT_SECONDARY_SORT_DESCENDING = "exportSecDescending",
EXPORT_TERTIARY_SORT_FIELD = "exportTerSort",
EXPORT_TERTIARY_SORT_DESCENDING = "exportTerDescending";
EXPORT_TERTIARY_SORT_DESCENDING = "exportTerDescending",
WRITEFIELD_SORTSTYLE = "writefieldSortStyle",
WRITEFIELD_USERDEFINEDORDER = "writefieldUserdefinedOrder",
WRITEFIELD_WRAPFIELD="wrapFieldLine";


// This String is used in the encoded list in prefs of external file type
// modifications, in order to indicate a removed default file type:
Expand Down Expand Up @@ -515,6 +519,12 @@ private JabRefPreferences() {
// behavior of JabRef before 2.10: both: false
defaults.put(WRITEFIELD_ADDSPACES, Boolean.TRUE);
defaults.put(WRITEFIELD_CAMELCASENAME, Boolean.TRUE);

//behavior of JabRef before LWang_AdjustableFieldOrder 1
//0 sorted order (2.10 default), 1 unsorted order (2.9.2 default), 2 user defined
defaults.put(WRITEFIELD_SORTSTYLE, 0);
defaults.put(WRITEFIELD_USERDEFINEDORDER, "author;title;journal;year;volume;number;pages;month;note;volume;pages;part;eid");
defaults.put(WRITEFIELD_WRAPFIELD, Boolean.FALSE);

defaults.put("useRemoteServer", Boolean.FALSE);
defaults.put("remoteServerPort", 6050);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class RightClickMenu extends JPopupMenu
rankingMenu = new JMenu(),
priorityMenu = new JMenu(),
typeMenu = new JMenu(Globals.lang("Change entry type"));
JMenuItem groupAdd, groupRemove;
JMenuItem groupAdd, groupRemove, groupMoveTo;
JCheckBoxMenuItem
floatMarked = new JCheckBoxMenuItem(Globals.lang("Float marked entries"),
Globals.prefs.getBoolean("floatMarkedEntries"));
Expand Down Expand Up @@ -323,16 +323,17 @@ public void actionPerformed(ActionEvent e) {
});
add(groupRemove);

add(new AbstractAction(Globals.lang("Remove from group"))
groupMoveTo=add(new AbstractAction(Globals.lang("move to group"))
{
public void actionPerformed(ActionEvent e) {
try {
panel.runCommand("removeFromGroup");
panel.runCommand("moveToGroup");
} catch (Throwable ex) {
logger.warning(ex.getMessage());
}
}
});
add(groupMoveTo);


floatMarked.addActionListener(new ActionListener() {
Expand Down
Loading

0 comments on commit 9ec813f

Please sign in to comment.