use of com.biglybt.pif.ui.UIPluginView in project BiglyBT by BiglySoftware.
the class TorrentUtil method calculateToolbarStates.
// XXX Don't think *View's need this call anymore. ToolBarView does it fo them
public static Map<String, Long> calculateToolbarStates(ISelectedContent[] currentContent, String viewID_unused) {
// System.out.println("calculateToolbarStates(" + currentContent.length + ", " + viewID_unused + " via " + Debug.getCompressedStackTrace());
/*
String[] TBKEYS = new String[] {
"download",
"play",
"stream",
"run",
"top",
"up",
"down",
"bottom",
"start",
"stop",
"remove"
};
*/
Map<String, Long> mapNewToolbarStates = new HashMap<>();
String[] itemsNeedingSelection = {};
String[] itemsNeedingRealDMSelection = { "remove", "top", "bottom", "transcode", "startstop" };
String[] itemsRequiring1DMwithHash = { "details", "comment", "up", "down" };
String[] itemsRequiring1DMSelection = {};
int numSelection = currentContent.length;
boolean hasSelection = numSelection > 0;
boolean has1Selection = numSelection == 1;
for (int i = 0; i < itemsNeedingSelection.length; i++) {
String itemID = itemsNeedingSelection[i];
mapNewToolbarStates.put(itemID, hasSelection ? UIToolBarItem.STATE_ENABLED : 0);
}
TableView tv = SelectedContentManager.getCurrentlySelectedTableView();
// not sure why we assume that the existance of any table view
boolean hasRealDM = tv != null;
if (!hasRealDM && numSelection > 0) {
hasRealDM = true;
for (int i = 0; i < currentContent.length; i++) {
ISelectedContent content = currentContent[i];
DownloadManager dm = content.getDownloadManager();
if (dm == null) {
hasRealDM = false;
break;
}
}
}
if (!hasRealDM) {
MultipleDocumentInterfaceSWT mdi = UIFunctionsManagerSWT.getUIFunctionsSWT().getMDISWT();
if (mdi != null) {
MdiEntrySWT entry = mdi.getCurrentEntrySWT();
if (entry != null) {
if (entry.getDatasource() instanceof DownloadManager) {
hasRealDM = true;
} else if ((entry instanceof UIPluginView) && (((UIPluginView) entry).getDataSource() instanceof DownloadManager)) {
hasRealDM = true;
}
}
}
}
boolean canStart = false;
boolean canStop = false;
boolean canRemoveFileInfo = false;
boolean canRunFileInfo = false;
boolean canCheckExist = false;
boolean hasDM = false;
boolean canRecheck = false;
if (currentContent.length > 0 && hasRealDM) {
// well, in fact, we can have hasRealDM set to true here (because tv isn't null) and actually not have a real dm.
// fancy that - protect against null DownloadManagers...
boolean canMoveUp = false;
boolean canMoveDown = false;
boolean canDownload = false;
canCheckExist = true;
GlobalManager gm = null;
for (int i = 0; i < currentContent.length; i++) {
ISelectedContent content = currentContent[i];
DownloadManager dm = content.getDownloadManager();
if (dm == null) {
if (!canDownload && content.getDownloadInfo() != null) {
canDownload = true;
}
continue;
}
if (gm == null) {
gm = dm.getGlobalManager();
}
int state = dm.getState();
canCheckExist &= (state == DownloadManager.STATE_ERROR || state == DownloadManager.STATE_STOPPED || state == DownloadManager.STATE_QUEUED);
int fileIndex = content.getFileIndex();
if (fileIndex == -1) {
if (!canMoveUp && gm.isMoveableUp(dm)) {
canMoveUp = true;
}
if (!canMoveDown && gm.isMoveableDown(dm)) {
canMoveDown = true;
}
hasDM = true;
if (!canStart && ManagerUtils.isStartable(dm)) {
canStart = true;
}
if (!canStop && ManagerUtils.isStopable(dm)) {
canStop = true;
}
} else {
DiskManagerFileInfoSet fileInfos = dm.getDiskManagerFileInfoSet();
if (fileIndex < fileInfos.nbFiles()) {
DiskManagerFileInfo fileInfo = fileInfos.getFiles()[fileIndex];
if (!canStart && (fileInfo.isSkipped())) {
canStart = true;
}
if (!canStop && !fileInfo.isSkipped()) {
canStop = true;
}
if (!canRemoveFileInfo && !fileInfo.isSkipped()) {
int storageType = fileInfo.getStorageType();
if (storageType == DiskManagerFileInfo.ST_LINEAR || storageType == DiskManagerFileInfo.ST_COMPACT) {
canRemoveFileInfo = true;
}
}
if (!canRunFileInfo && fileInfo.getAccessMode() == DiskManagerFileInfo.READ && fileInfo.getDownloaded() == fileInfo.getLength() && fileInfo.getFile(true).exists()) {
canRunFileInfo = true;
}
}
}
canRecheck = canRecheck || dm.canForceRecheck();
}
boolean canRemove = hasDM || canRemoveFileInfo;
mapNewToolbarStates.put("remove", canRemove ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put("download", canDownload ? UIToolBarItem.STATE_ENABLED : 0);
if (currentContent.length == 1) {
mapNewToolbarStates.put("up", canMoveUp ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put("down", canMoveDown ? UIToolBarItem.STATE_ENABLED : 0);
}
}
boolean canRun = has1Selection && ((hasDM && !canRunFileInfo) || (!hasDM && canRunFileInfo));
if (canRun) {
ISelectedContent content = currentContent[0];
DownloadManager dm = content.getDownloadManager();
if (dm == null) {
canRun = false;
} else {
TOTorrent torrent = dm.getTorrent();
if (torrent == null) {
canRun = false;
} else if (!dm.getAssumedComplete() && torrent.isSimpleTorrent()) {
canRun = false;
/*
} else if (PlatformTorrentUtils.useEMP(torrent)
&& PlatformTorrentUtils.embeddedPlayerAvail()
&& PlayUtils.canProgressiveOrIsComplete(torrent)) {
// play button enabled and not UMP.. don't need launch
canRun = false;
}
*/
}
}
}
mapNewToolbarStates.put("run", canRun ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put("start", canStart ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put("stop", canStop ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put("startstop", canStart || canStop ? UIToolBarItem.STATE_ENABLED : 0);
for (int i = 0; i < itemsNeedingRealDMSelection.length; i++) {
String itemID = itemsNeedingRealDMSelection[i];
if (!mapNewToolbarStates.containsKey(itemID)) {
mapNewToolbarStates.put(itemID, hasSelection && hasDM && hasRealDM ? UIToolBarItem.STATE_ENABLED : 0);
}
}
for (int i = 0; i < itemsRequiring1DMSelection.length; i++) {
String itemID = itemsRequiring1DMSelection[i];
if (!mapNewToolbarStates.containsKey(itemID)) {
mapNewToolbarStates.put(itemID, has1Selection && hasDM ? UIToolBarItem.STATE_ENABLED : 0);
}
}
for (int i = 0; i < itemsRequiring1DMwithHash.length; i++) {
String itemID = itemsRequiring1DMwithHash[i];
if (!mapNewToolbarStates.containsKey(itemID)) {
mapNewToolbarStates.put(itemID, hasDM ? UIToolBarItem.STATE_ENABLED : 0);
}
}
mapNewToolbarStates.put("download", has1Selection && (!(currentContent[0] instanceof ISelectedVuzeFileContent)) && currentContent[0].getDownloadManager() == null && (currentContent[0].getHash() != null || currentContent[0].getDownloadInfo() != null) ? UIToolBarItem.STATE_ENABLED : 0);
if (tv != null) {
TableColumn tc = tv.getTableColumn(RankItem.COLUMN_ID);
if (tc != null && !tc.isVisible()) {
mapNewToolbarStates.put("up", 0L);
mapNewToolbarStates.put("down", 0L);
}
}
mapNewToolbarStates.put(TU_ITEM_RECHECK, canRecheck ? UIToolBarItem.STATE_ENABLED : 0);
mapNewToolbarStates.put(TU_ITEM_CHECK_FILES, canCheckExist ? UIToolBarItem.STATE_ENABLED : 0);
return mapNewToolbarStates;
}
Aggregations