📄 cmschnav.java
字号:
// do not include the current file
if(!res.getAbsolutePath().equals(filename)){
// check if the resource is not marked as deleted
if(res.getState() != C_STATE_DELETED) {
String navpos = cms.readProperty(res.getAbsolutePath(), C_PROPERTY_NAVPOS);
// check if there is a navpos for this file/folder
if(navpos != null) {
nicename = Encoder.escapeHtml(cms.readProperty(res.getAbsolutePath(), C_PROPERTY_NAVTEXT));
if(nicename == null) {
nicename = res.getName();
}
// add this file/folder to the storage.
filenames[count] = res.getAbsolutePath();
nicenames[count] = nicename;
positions[count] = navpos;
if(new Float(navpos).floatValue() > max) {
max = new Float(navpos).floatValue();
}
count++;
}
}
}
}
} else {
filenames = new String[3];
nicenames = new String[3];
positions = new String[3];
}
// now add the first and last value
filenames[0] = "FIRSTENTRY";
nicenames[0] = lang.getLanguageValue("input.firstelement");
positions[0] = "0";
filenames[count] = "LASTENTRY";
nicenames[count] = lang.getLanguageValue("input.lastelement");
positions[count] = new Float(max + 1).toString();
// add the default value for no change of navigation position
filenames[count+1] = "NOCHANGE";
nicenames[count+1] = lang.getLanguageValue("input.nochange");
positions[count+1] = "-1";
// finally sort the nav information.
sort(cms, filenames, nicenames, positions, count);
// put all arrays into a hashtable to return them to the calling method.
storage.put("FILENAMES", filenames);
storage.put("NICENAMES", nicenames);
storage.put("POSITIONS", positions);
// the value for count includes the entry for no change
storage.put("COUNT", new Integer(count+1));
return storage;
}
/**
* Gets the files displayed in the navigation select box.
* @param cms The CmsObject.
* @param lang The langauge definitions.
* @param names The names of the new rescources.
* @param values The links that are connected with each resource.
* @param parameters Hashtable of parameters (not used yet).
* @return The vectors names and values are filled with data for building the navigation.
* @throws Throws CmsException if something goes wrong.
*/
public Integer getNavPos(CmsObject cms, CmsXmlLanguageFile lang, Vector names,
Vector values, Hashtable parameters) throws CmsException {
I_CmsSession session = cms.getRequestContext().getSession(true);
String preselect = (String)session.getValue(C_SESSIONHEADER + C_PARA_NAVPOS);
int retValue = -1;
// get the name of the current file
String filename = (String)parameters.get(C_PARA_FILE);
if(filename == null || "".equals(filename)){
filename = (String)session.getValue(C_SESSIONHEADER + C_PARA_FILE);
}
if (filename != null) {
filename = filename.trim();
}
// get the nav information
Hashtable storage = getNavData(cms, filename);
if(storage.size() > 0) {
String[] nicenames = (String[])storage.get("NICENAMES");
int count = ((Integer)storage.get("COUNT")).intValue();
// finally fill the result vectors
for(int i = 0;i <= count;i++) {
names.addElement(nicenames[i]);
values.addElement(nicenames[i]);
if ((preselect != null) && (Encoder.escapeHtml(preselect).equals(nicenames[i]))){
retValue = values.size() -1;
}
}
}
else {
values = new Vector();
}
if (retValue == -1){
// set the default value to no change
return new Integer(values.size() - 1);
}else{
return new Integer(retValue);
}
}
/**
* Indicates if the results of this class are cacheable.
*
* @param cms CmsObject Object for accessing system resources
* @param templateFile Filename of the template file
* @param elementName Element name of this template in our parent template.
* @param parameters Hashtable with all template class parameters.
* @param templateSelector template section that should be processed.
* @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
*/
public boolean isCacheable(CmsObject cms, String templateFile, String elementName,
Hashtable parameters, String templateSelector) {
return false;
}
/**
* Sorts a set of three String arrays containing navigation information depending on
* their navigation positions.
* @param cms Cms Object for accessign files.
* @param filenames Array of filenames
* @param nicenames Array of well formed navigation names
* @param positions Array of navpostions
*/
private void sort(CmsObject cms, String[] filenames, String[] nicenames, String[] positions, int max) {
// Sorting algorithm
// This method uses an bubble sort, so replace this with something more
// efficient
for(int i = max - 1;i > 1;i--) {
for(int j = 1;j < i;j++) {
float a = new Float(positions[j]).floatValue();
float b = new Float(positions[j + 1]).floatValue();
if(a > b) {
String tempfilename = filenames[j];
String tempnicename = nicenames[j];
String tempposition = positions[j];
filenames[j] = filenames[j + 1];
nicenames[j] = nicenames[j + 1];
positions[j] = positions[j + 1];
filenames[j + 1] = tempfilename;
nicenames[j + 1] = tempnicename;
positions[j + 1] = tempposition;
}
}
}
}
/**
* Updates the navigation position of all resources in the actual folder.
* @param cms The CmsObject.
* @param newfile The new file added to the nav.
* @param navpos The file after which the new entry is sorted.
*/
private void updateNavPos(CmsObject cms, CmsResource curResource, String navpos) throws CmsException {
float newPos = 0;
boolean changePos = true;
// get the nav information
Hashtable storage = getNavData(cms, curResource.getAbsolutePath());
if(storage.size() > 0) {
String[] nicenames = (String[])storage.get("NICENAMES");
String[] positions = (String[])storage.get("POSITIONS");
// the value for count must not include the entry for no change
int count = ((Integer)storage.get("COUNT")).intValue()-1;
// now find the file after which the new file is sorted
int pos = 0;
for(int i = 0;i < nicenames.length;i++) {
if(Encoder.escapeHtml(navpos).equals((String)nicenames[i])) {
pos = i;
}
}
// check if the value for no change is selected
if("-1".equals(positions[pos])){
changePos = false;
}
// only get new position if a new position was selected
if(changePos){
if(pos < count) {
float low = new Float(positions[pos]).floatValue();
float high = new Float(positions[pos + 1]).floatValue();
newPos = (high + low) / 2;
} else {
newPos = new Float(positions[pos]).floatValue() + 1;
}
}
} else {
newPos = 1;
}
// only change position if new position was selected
if(changePos){
cms.writeProperty(curResource.getAbsolutePath(), C_PROPERTY_NAVPOS, new Float(newPos).toString());
}
}
/**
* removes all session parameters used by this class.
* @param session The session.
*/
private void clearSession(I_CmsSession session){
session.removeValue(C_SESSIONHEADER + C_PARA_NAVPOS);
session.removeValue(C_SESSIONHEADER + C_PARA_FILE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -